/// <summary> /// Filters a error location items /// </summary> /// <param name="errorLocationItems">An array of <see cref="ErrorLocationItem"/> instances</param> private static ErrorLocationItem[] FilterErrorLocationItems(ErrorLocationItem[] errorLocationItems) { if (errorLocationItems.Length == 0) { return(errorLocationItems); } int excessErrorLocationItemIndex = 0; foreach (ErrorLocationItem item in errorLocationItems) { string documentName = item.DocumentName; string functionName = item.FunctionName; if (documentName == "vm.js" || documentName == GENERATED_FUNCTION_CALL_FILE_NAME || (documentName == "anonymous" && functionName == "callFunction")) { break; } excessErrorLocationItemIndex++; } var processedErrorLocationItems = new ErrorLocationItem[excessErrorLocationItemIndex]; Array.Copy(errorLocationItems, processedErrorLocationItems, excessErrorLocationItemIndex); return(processedErrorLocationItems); }
/// <summary> /// Parses a string representation of the script error location to produce an array of /// <see cref="ErrorLocationItem"/> instances /// </summary> /// <param name="errorLocation">String representation of the script error location</param> /// <returns>An array of <see cref="ErrorLocationItem"/> instances</returns> public static ErrorLocationItem[] ParseErrorLocation(string errorLocation) { if (string.IsNullOrWhiteSpace(errorLocation)) { return(new ErrorLocationItem[0]); } var errorLocationItems = new List <ErrorLocationItem>(); string[] lines = errorLocation.SplitToLines(); int lineCount = lines.Length; for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) { string line = lines[lineIndex]; if (line.Length == 0) { continue; } Match lineMatch = _errorLocationLineRegex.Match(line); if (lineMatch.Success) { GroupCollection lineGroups = lineMatch.Groups; Group lineNumberGroup = lineGroups["lineNumber"]; Group columnNumberGroup = lineGroups["columnNumber"]; var errorLocationItem = new ErrorLocationItem { FunctionName = lineGroups["functionName"].Value, DocumentName = lineGroups["documentName"].Value, LineNumber = lineNumberGroup.Success ? int.Parse(lineNumberGroup.Value) : 0, ColumnNumber = columnNumberGroup.Success ? int.Parse(columnNumberGroup.Value) : 0, SourceFragment = lineGroups["sourceFragment"].Value }; errorLocationItems.Add(errorLocationItem); } else { Debug.WriteLine(string.Format(CoreStrings.Runtime_InvalidErrorLocationLineFormat, line)); return(new ErrorLocationItem[0]); } } return(errorLocationItems.ToArray()); }
private static WrapperException WrapScriptEngineException(OriginalException originalException) { WrapperException wrapperException; string message = originalException.Message; string messageWithErrorLocation = originalException.ErrorDetails; string description = message; string type = string.Empty; string documentName = string.Empty; int lineNumber = 0; int columnNumber = 0; string callStack = string.Empty; string sourceFragment = string.Empty; if (originalException.IsFatal) { if (message == "The V8 runtime has exceeded its memory limit") { wrapperException = new WrapperRuntimeException(message, EngineName, EngineVersion, originalException); } else { wrapperException = new WrapperFatalException(message, EngineName, EngineVersion, originalException); } } else { Match messageWithTypeMatch = _errorMessageWithTypeRegex.Match(message); if (messageWithTypeMatch.Success) { GroupCollection messageWithTypeGroups = messageWithTypeMatch.Groups; type = messageWithTypeGroups["type"].Value; description = messageWithTypeGroups["description"].Value; var errorLocationItems = new ErrorLocationItem[0]; if (message.Length < messageWithErrorLocation.Length) { string errorLocation = messageWithErrorLocation .TrimStart(message) .TrimStart(new char[] { '\n', '\r' }) ; errorLocationItems = JsErrorHelpers.ParseErrorLocation(errorLocation); if (errorLocationItems.Length > 0) { ErrorLocationItem firstErrorLocationItem = errorLocationItems[0]; documentName = firstErrorLocationItem.DocumentName; lineNumber = firstErrorLocationItem.LineNumber; columnNumber = firstErrorLocationItem.ColumnNumber; string sourceLine = firstErrorLocationItem.SourceFragment; sourceFragment = TextHelpers.GetTextFragmentFromLine(sourceLine, columnNumber); firstErrorLocationItem.SourceFragment = sourceFragment; } } WrapperScriptException wrapperScriptException; if (type == JsErrorType.Syntax) { message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber, columnNumber, sourceFragment); wrapperScriptException = new WrapperCompilationException(message, EngineName, EngineVersion, originalException); } else { callStack = JsErrorHelpers.StringifyErrorLocationItems(errorLocationItems, true); string callStackWithSourceFragment = JsErrorHelpers.StringifyErrorLocationItems( errorLocationItems); message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStackWithSourceFragment); wrapperScriptException = new WrapperRuntimeException(message, EngineName, EngineVersion, originalException) { CallStack = callStack }; } wrapperScriptException.Type = type; wrapperScriptException.DocumentName = documentName; wrapperScriptException.LineNumber = lineNumber; wrapperScriptException.ColumnNumber = columnNumber; wrapperScriptException.SourceFragment = sourceFragment; wrapperException = wrapperScriptException; } else { wrapperException = new WrapperException(message, EngineName, EngineVersion, originalException); } } wrapperException.Description = description; return(wrapperException); }
private WrapperException WrapInvocationException(InvocationException originalException) { WrapperException wrapperException; string message = originalException.Message; string description = string.Empty; if (_timeoutErrorMessage.IsMatch(message)) { message = CoreStrings.Runtime_ScriptTimeoutExceeded; description = message; var wrapperTimeoutException = new WrapperTimeoutException(message, EngineName, _engineVersion, originalException) { Description = description }; return(wrapperTimeoutException); } string documentName = string.Empty; int lineNumber = 0; int columnNumber = 0; string sourceLine = string.Empty; Match detailsMatch = _errorDetailsRegex.Match(message); int detailsLength = 0; if (detailsMatch.Success) { GroupCollection detailsGroups = detailsMatch.Groups; description = detailsGroups["description"].Value; documentName = detailsGroups["documentName"].Success ? detailsGroups["documentName"].Value : string.Empty; lineNumber = detailsGroups["lineNumber"].Success ? int.Parse(detailsGroups["lineNumber"].Value) : 0; columnNumber = NodeJsErrorHelpers.GetColumnCountFromLine(detailsGroups["pointer"].Value); sourceLine = detailsGroups["sourceLine"].Value; detailsLength = detailsMatch.Length; } message = detailsLength > 0 ? message.Substring(detailsLength) : message; Match messageWithTypeMatch = _errorMessageWithTypeRegex.Match(message); if (messageWithTypeMatch.Success) { GroupCollection messageWithTypeGroups = messageWithTypeMatch.Groups; string type = messageWithTypeGroups["type"].Value; description = messageWithTypeGroups["description"].Value; string sourceFragment = TextHelpers.GetTextFragmentFromLine(sourceLine, columnNumber); WrapperScriptException wrapperScriptException; if (type == JsErrorType.Syntax) { message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber, columnNumber, sourceFragment); wrapperScriptException = new WrapperCompilationException(message, EngineName, _engineVersion, originalException); } else if (type == "UsageError") { wrapperException = new WrapperUsageException(description, EngineName, _engineVersion, originalException); wrapperException.Description = description; return(wrapperException); } else { var errorLocationItems = new ErrorLocationItem[0]; int messageLength = message.Length; int messageWithTypeLength = messageWithTypeMatch.Length; if (messageWithTypeLength < messageLength) { string errorLocation = message.Substring(messageWithTypeLength); errorLocationItems = NodeJsErrorHelpers.ParseErrorLocation(errorLocation); errorLocationItems = FilterErrorLocationItems(errorLocationItems); if (errorLocationItems.Length > 0) { ErrorLocationItem firstErrorLocationItem = errorLocationItems[0]; documentName = firstErrorLocationItem.DocumentName; lineNumber = firstErrorLocationItem.LineNumber; columnNumber = firstErrorLocationItem.ColumnNumber; firstErrorLocationItem.SourceFragment = sourceFragment; } } string callStack = JsErrorHelpers.StringifyErrorLocationItems(errorLocationItems, true); string callStackWithSourceFragment = JsErrorHelpers.StringifyErrorLocationItems( errorLocationItems); message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStackWithSourceFragment); wrapperScriptException = new WrapperRuntimeException(message, EngineName, _engineVersion, originalException) { CallStack = callStack }; } wrapperScriptException.Type = type; wrapperScriptException.DocumentName = documentName; wrapperScriptException.LineNumber = lineNumber; wrapperScriptException.ColumnNumber = columnNumber; wrapperScriptException.SourceFragment = sourceFragment; wrapperException = wrapperScriptException; } else { wrapperException = new WrapperException(message, EngineName, _engineVersion, originalException); } wrapperException.Description = description; return(wrapperException); }