|
9 | 9 |
|
10 | 10 | using JavaScriptEngineSwitcher.Core.Extensions; |
11 | 11 | using JavaScriptEngineSwitcher.Core.Resources; |
| 12 | +using JavaScriptEngineSwitcher.Core.Utilities; |
12 | 13 |
|
13 | 14 | namespace JavaScriptEngineSwitcher.Core.Helpers |
14 | 15 | { |
@@ -39,53 +40,107 @@ public static class JsErrorHelpers |
39 | 40 | @")" + |
40 | 41 | @"(?: -> (?<sourceFragment>[^\n\r]+))?$"); |
41 | 42 |
|
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets a string representation of the script error location |
| 46 | + /// </summary> |
| 47 | + /// <param name="message">Error message with the script error location</param> |
| 48 | + /// <param name="messageWithoutErrorLocation">Error message without the script error location</param> |
| 49 | + /// <returns>String representation of the script error location</returns> |
| 50 | + public static string GetErrorLocationFromMessage(string message, string messageWithoutErrorLocation) |
| 51 | + { |
| 52 | + if (string.IsNullOrWhiteSpace(message)) |
| 53 | + { |
| 54 | + return string.Empty; |
| 55 | + } |
| 56 | + |
| 57 | + if (string.IsNullOrWhiteSpace(messageWithoutErrorLocation)) |
| 58 | + { |
| 59 | + return message; |
| 60 | + } |
| 61 | + |
| 62 | + string errorLocation = message |
| 63 | + .TrimStart(messageWithoutErrorLocation) |
| 64 | + .TrimStart(EnvironmentShortcuts.NewLineChars) |
| 65 | + ; |
| 66 | + |
| 67 | + return errorLocation; |
| 68 | + } |
| 69 | + |
42 | 70 | /// <summary> |
43 | 71 | /// Parses a string representation of the script error location to produce an array of |
44 | 72 | /// <see cref="ErrorLocationItem"/> instances |
45 | 73 | /// </summary> |
46 | 74 | /// <param name="errorLocation">String representation of the script error location</param> |
47 | 75 | /// <returns>An array of <see cref="ErrorLocationItem"/> instances</returns> |
48 | 76 | public static ErrorLocationItem[] ParseErrorLocation(string errorLocation) |
| 77 | + { |
| 78 | + return ParseErrorLocation(errorLocation, MapErrorLocationItem); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Parses a string representation of the script error location to produce an array of |
| 83 | + /// <see cref="ErrorLocationItem"/> instances |
| 84 | + /// </summary> |
| 85 | + /// <param name="errorLocation">String representation of the script error location</param> |
| 86 | + /// <param name="itemMapper">Error location item mapper</param> |
| 87 | + /// <returns>An array of <see cref="ErrorLocationItem"/> instances</returns> |
| 88 | + public static ErrorLocationItem[] ParseErrorLocation(string errorLocation, |
| 89 | + Func<string, ErrorLocationItem> itemMapper) |
49 | 90 | { |
50 | 91 | if (string.IsNullOrWhiteSpace(errorLocation)) |
51 | 92 | { |
52 | | - return new ErrorLocationItem[0]; |
| 93 | + return []; |
53 | 94 | } |
54 | 95 |
|
55 | | - var errorLocationItems = new List<ErrorLocationItem>(); |
56 | | - string[] lines = errorLocation.SplitToLines(); |
| 96 | + if (itemMapper is null) |
| 97 | + { |
| 98 | + throw new ArgumentNullException(nameof(itemMapper)); |
| 99 | + } |
| 100 | + |
| 101 | + string[] lines = errorLocation.SplitToLines(StringSplitOptions.RemoveEmptyEntries); |
57 | 102 | int lineCount = lines.Length; |
| 103 | + var errorLocationItems = new List<ErrorLocationItem>(lineCount); |
58 | 104 |
|
59 | | - for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) |
| 105 | + foreach (string line in lines) |
60 | 106 | { |
61 | | - string line = lines[lineIndex]; |
62 | | - Match lineMatch = _errorLocationLineRegex.Match(line); |
63 | | - |
64 | | - if (lineMatch.Success) |
| 107 | + ErrorLocationItem errorLocationItem = itemMapper(line); |
| 108 | + if (errorLocationItem is not null) |
65 | 109 | { |
66 | | - GroupCollection lineGroups = lineMatch.Groups; |
67 | | - |
68 | | - var errorLocationItem = new ErrorLocationItem |
69 | | - { |
70 | | - FunctionName = lineGroups["functionName"].Value, |
71 | | - DocumentName = lineGroups["documentName"].Value, |
72 | | - LineNumber = int.Parse(lineGroups["lineNumber"].Value), |
73 | | - ColumnNumber = lineGroups["columnNumber"].Success ? |
74 | | - int.Parse(lineGroups["columnNumber"].Value) : 0, |
75 | | - SourceFragment = lineGroups["sourceFragment"].Value |
76 | | - }; |
77 | 110 | errorLocationItems.Add(errorLocationItem); |
78 | 111 | } |
79 | 112 | else |
80 | 113 | { |
81 | 114 | Debug.WriteLine(string.Format(Strings.Runtime_InvalidErrorLocationLineFormat, line)); |
82 | | - return new ErrorLocationItem[0]; |
| 115 | + return []; |
83 | 116 | } |
84 | 117 | } |
85 | 118 |
|
86 | 119 | return errorLocationItems.ToArray(); |
87 | 120 | } |
88 | 121 |
|
| 122 | + private static ErrorLocationItem MapErrorLocationItem(string errorLocationLine) |
| 123 | + { |
| 124 | + ErrorLocationItem item = null; |
| 125 | + Match lineMatch = _errorLocationLineRegex.Match(errorLocationLine); |
| 126 | + |
| 127 | + if (lineMatch.Success) |
| 128 | + { |
| 129 | + GroupCollection lineGroups = lineMatch.Groups; |
| 130 | + item = new ErrorLocationItem |
| 131 | + { |
| 132 | + FunctionName = lineGroups["functionName"].Value, |
| 133 | + DocumentName = lineGroups["documentName"].Value, |
| 134 | + LineNumber = int.Parse(lineGroups["lineNumber"].Value), |
| 135 | + ColumnNumber = lineGroups["columnNumber"].Success ? |
| 136 | + int.Parse(lineGroups["columnNumber"].Value) : 0, |
| 137 | + SourceFragment = lineGroups["sourceFragment"].Value |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + return item; |
| 142 | + } |
| 143 | + |
89 | 144 | /// <summary> |
90 | 145 | /// Produces a string representation of the script error location from array of |
91 | 146 | /// <see cref="ErrorLocationItem"/> instances |
|
0 commit comments