private static WrapperException WrapJsException(OriginalException originalException)
        {
            WrapperException wrapperException;
            string           message      = originalException.Message;
            string           description  = message;
            string           type         = originalException.Type;
            string           documentName = originalException.Resource;
            int lineNumber   = originalException.Line;
            int columnNumber = originalException.Column;

            if (originalException is OriginalInteropException)
            {
                wrapperException = new WrapperException(message, EngineName, EngineVersion, originalException);
            }
            else if (type == null && message.Equals(":  at line: 0 column: 1.", StringComparison.Ordinal))
            {
                wrapperException = new WrapperInterruptedException(CoreStrings.Runtime_ScriptInterrupted,
                                                                   EngineName, EngineVersion, originalException);
            }
            else
            {
                Match scriptErrorMessageMatch = _scriptErrorMessageRegex.Match(message);
                if (scriptErrorMessageMatch.Success)
                {
                    WrapperScriptException wrapperScriptException;
                    description = scriptErrorMessageMatch.Groups["description"].Value;
                    message     = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName,
                                                                            lineNumber, columnNumber);

                    if (type == JsErrorType.Syntax)
                    {
                        wrapperScriptException = new WrapperCompilationException(message,
                                                                                 EngineName, EngineVersion, originalException);
                    }
                    else
                    {
                        wrapperScriptException = new WrapperRuntimeException(message,
                                                                             EngineName, EngineVersion, originalException);
                    }
                    wrapperScriptException.Type         = type;
                    wrapperScriptException.DocumentName = documentName;
                    wrapperScriptException.LineNumber   = lineNumber;
                    wrapperScriptException.ColumnNumber = columnNumber;

                    wrapperException = wrapperScriptException;
                }
                else
                {
                    wrapperException = new WrapperException(message, EngineName, EngineVersion,
                                                            originalException);
                }
            }

            wrapperException.Description = description;

            return(wrapperException);
        }
        private static WrapperException WrapJavaScriptException(
            OriginalJavaScriptException originalJavaScriptException)
        {
            WrapperException wrapperException;
            string           message = originalJavaScriptException.Message;

            if (string.IsNullOrWhiteSpace(message))
            {
                message = "An unknown error occurred";
            }
            string description  = message;
            string type         = string.Empty;
            string documentName = originalJavaScriptException.Location.Source;
            int    lineNumber   = originalJavaScriptException.LineNumber;
            int    columnNumber = originalJavaScriptException.Column + 1;

            OriginalValue errorValue = originalJavaScriptException.Error;

            if (errorValue.IsObject())
            {
                OriginalObjectInstance errorObject = errorValue.AsObject();

                OriginalValue namePropertyValue = errorObject.Get("name");
                if (namePropertyValue.IsString())
                {
                    type = namePropertyValue.AsString();
                }
            }

            if (!string.IsNullOrEmpty(type))
            {
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber,
                                                                    columnNumber);

                var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                          originalJavaScriptException)
                {
                    Type         = type,
                    DocumentName = documentName,
                    LineNumber   = lineNumber,
                    ColumnNumber = columnNumber
                };

                wrapperException = wrapperRuntimeException;
            }
            else
            {
                wrapperException = new WrapperException(message, EngineName, EngineVersion,
                                                        originalJavaScriptException);
            }

            wrapperException.Description = description;

            return(wrapperException);
        }
        private static WrapperRuntimeException WrapStatementsCountOverflowException(
            OriginalStatementsCountOverflowException originalStatementsException)
        {
            string description = originalStatementsException.Message;
            string type        = JsErrorType.Range;
            string message     = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

            var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalStatementsException)
            {
                Description = description
            };

            return(wrapperRuntimeException);
        }
        private static WrapperRuntimeException WrapMemoryLimitExceededException(
            OriginalMemoryLimitExceededException originalMemoryException)
        {
            string description = originalMemoryException.Message;
            string type        = JsErrorType.Common;
            string message     = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

            var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalMemoryException)
            {
                Description = description
            };

            return(wrapperRuntimeException);
        }
        private static WrapperRuntimeException WrapRecursionDepthOverflowException(
            OriginalRecursionDepthOverflowException originalRecursionException)
        {
            string callStack = string.Empty;

            string[] callChainItems = originalRecursionException.CallChain
                                      .Split(new string[] { "->" }, StringSplitOptions.None)
            ;

            if (callChainItems.Length > 0)
            {
                var           stringBuilderPool = StringBuilderPool.Shared;
                StringBuilder stackBuilder      = stringBuilderPool.Rent();

                for (int chainItemIndex = callChainItems.Length - 1; chainItemIndex >= 0; chainItemIndex--)
                {
                    string chainItem = callChainItems[chainItemIndex];
                    if (chainItem == "anonymous function")
                    {
                        chainItem = "Anonymous function";
                    }

                    JsErrorHelpers.WriteErrorLocationLine(stackBuilder, chainItem, string.Empty, 0, 0);
                    if (chainItemIndex > 0)
                    {
                        stackBuilder.AppendLine();
                    }
                }

                callStack = stackBuilder.ToString();
                stringBuilderPool.Return(stackBuilder);
            }

            string description = originalRecursionException.Message;
            string type        = JsErrorType.Range;
            string message     = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStack);

            var wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRecursionException)
            {
                Description = description,
                Type        = type,
                CallStack   = callStack
            };

            return(wrapperRuntimeException);
        }
        private static WrapperCompilationException WrapSyntaxException(
            OriginalSyntaxException originalSyntaxException)
        {
            string description  = originalSyntaxException.Message;
            string type         = JsErrorType.Syntax;
            string documentName = originalSyntaxException.SourcePath;
            int    lineNumber   = originalSyntaxException.LineNumber;
            string message      = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName,
                                                                            lineNumber, 0);

            var wrapperCompilationException = new WrapperCompilationException(message, EngineName, EngineVersion,
                                                                              originalSyntaxException)
            {
                Description  = description,
                Type         = type,
                DocumentName = documentName,
                LineNumber   = lineNumber
            };

            return(wrapperCompilationException);
        }
        private static WrapperCompilationException WrapParserException(OriginalParserException originalParserException)
        {
            string description  = originalParserException.Description;
            string type         = JsErrorType.Syntax;
            string documentName = originalParserException.SourceText;
            int    lineNumber   = originalParserException.LineNumber;
            int    columnNumber = originalParserException.Column;
            string message      = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber,
                                                                            columnNumber);

            var wrapperCompilationException = new WrapperCompilationException(message, EngineName, EngineVersion,
                                                                              originalParserException)
            {
                Description  = description,
                Type         = type,
                DocumentName = documentName,
                LineNumber   = lineNumber,
                ColumnNumber = columnNumber
            };

            return(wrapperCompilationException);
        }
Example #8
0
        private static WrapperException WrapJsException(OriginalException originalException)
        {
            WrapperException wrapperException;
            string           message     = originalException.Message;
            string           description = message;
            string           type        = string.Empty;
            int    lineNumber            = 0;
            int    columnNumber          = 0;
            string sourceFragment        = string.Empty;

            var errorValue = originalException.Error?.Value as OriginalError;

            if (errorValue != null)
            {
                message     = errorValue.message.As <string>();
                description = message;
                type        = errorValue.name.As <string>();
            }

            if (!string.IsNullOrEmpty(type))
            {
                WrapperScriptException wrapperScriptException;
                if (type == JsErrorType.Syntax)
                {
                    Match messageMatch = _syntaxErrorMessageRegex.Match(message);
                    if (messageMatch.Success)
                    {
                        GroupCollection messageGroups = messageMatch.Groups;
                        description  = messageGroups["description"].Value;
                        lineNumber   = int.Parse(messageGroups["lineNumber"].Value);
                        columnNumber = int.Parse(messageGroups["columnNumber"].Value);
                    }
                    message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty,
                                                                        lineNumber, columnNumber);

                    wrapperScriptException = new WrapperCompilationException(message, EngineName, EngineVersion,
                                                                             originalException);
                }
                else
                {
                    string sourceCode = originalException.Code;
                    OriginalCodeCoordinates codeCoordinates = originalException.CodeCoordinates;
                    if (codeCoordinates != null)
                    {
                        lineNumber   = codeCoordinates.Line;
                        columnNumber = codeCoordinates.Column;
                    }
                    sourceFragment = TextHelpers.GetTextFragment(sourceCode, lineNumber, columnNumber);
                    message        = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty,
                                                                               lineNumber, columnNumber, sourceFragment);

                    wrapperScriptException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                         originalException);
                }
                wrapperScriptException.Type           = type;
                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 static WrapperException WrapJavaScriptException(
            OriginalJavaScriptException originalJavaScriptException)
        {
            WrapperException wrapperException;
            string           message = originalJavaScriptException.Message;
            string           messageWithCallStack = string.Empty;
            string           description          = message;
            string           type         = originalJavaScriptException.Name;
            string           documentName = originalJavaScriptException.SourcePath ?? string.Empty;
            int    lineNumber             = originalJavaScriptException.LineNumber;
            string callStack = string.Empty;

            var errorValue = originalJavaScriptException.ErrorObject as OriginalErrorInstance;

            if (errorValue != null)
            {
                messageWithCallStack = errorValue.Stack;
                description          = !string.IsNullOrEmpty(errorValue.Message) ?
                                       errorValue.Message : description;
            }

            if (!string.IsNullOrEmpty(type))
            {
                WrapperScriptException wrapperScriptException;
                if (type == JsErrorType.Syntax)
                {
                    message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName,
                                                                        lineNumber, 0);

                    wrapperScriptException = new WrapperCompilationException(message, EngineName, EngineVersion,
                                                                             originalJavaScriptException);
                }
                else
                {
                    if (message.Length < messageWithCallStack.Length)
                    {
                        string rawCallStack = messageWithCallStack
                                              .TrimStart(message)
                                              .TrimStart(new char[] { '\n', '\r' })
                        ;
                        ErrorLocationItem[] callStackItems = JsErrorHelpers.ParseErrorLocation(rawCallStack);

                        if (callStackItems.Length > 0)
                        {
                            FixCallStackItems(callStackItems);
                            callStack = JsErrorHelpers.StringifyErrorLocationItems(callStackItems);

                            if (string.IsNullOrWhiteSpace(documentName))
                            {
                                documentName = callStackItems[0].DocumentName;
                            }
                        }
                    }

                    message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStack);

                    wrapperScriptException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                         originalJavaScriptException)
                    {
                        CallStack = callStack
                    };
                }
                wrapperScriptException.Type         = type;
                wrapperScriptException.DocumentName = documentName;
                wrapperScriptException.LineNumber   = lineNumber;

                wrapperException = wrapperScriptException;
            }
            else
            {
                wrapperException = new WrapperException(message, EngineName, EngineVersion,
                                                        originalJavaScriptException);
            }

            wrapperException.Description = description;

            return(wrapperException);
        }
        private WrapperRuntimeException WrapRuntimeException(OriginalRuntimeException originalRuntimeException)
        {
            WrapperRuntimeException wrapperRuntimeException;
            string message = originalRuntimeException.Message;

            if (string.IsNullOrWhiteSpace(message))
            {
                message = "An unknown error occurred";
            }
            string description  = message;
            string type         = string.Empty;
            string documentName = string.Empty;
            int    lineNumber   = 0;
            int    columnNumber = 0;
            string callStack    = string.Empty;

            if (originalRuntimeException is OriginalJavaScriptException)
            {
                var originalJavaScriptException = (OriginalJavaScriptException)originalRuntimeException;
                documentName = originalJavaScriptException.Location.Source;
                lineNumber   = originalJavaScriptException.LineNumber;
                columnNumber = originalJavaScriptException.Column + 1;

                OriginalValue errorValue = originalJavaScriptException.Error;
                if (errorValue.IsObject())
                {
                    OriginalObjectInstance errorObject = errorValue.AsObject();

                    OriginalValue namePropertyValue = errorObject.Get("name");
                    if (namePropertyValue.IsString())
                    {
                        type = namePropertyValue.AsString();
                    }
                }

                if (string.IsNullOrEmpty(type))
                {
                    type = JsErrorType.Common;
                }

                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName, lineNumber,
                                                                    columnNumber);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalJavaScriptException);
            }
            else if (originalRuntimeException is OriginalMemoryLimitExceededException)
            {
                type    = JsErrorType.Common;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }
            else if (originalRuntimeException is OriginalRecursionDepthOverflowException)
            {
                var      originalRecursionException = (OriginalRecursionDepthOverflowException)originalRuntimeException;
                string[] callChainItems             = originalRecursionException.CallChain
                                                      .Split(new string[] { "->" }, StringSplitOptions.None)
                ;

                if (callChainItems.Length > 0)
                {
                    var           stringBuilderPool = StringBuilderPool.Shared;
                    StringBuilder stackBuilder      = stringBuilderPool.Rent();

                    for (int chainItemIndex = callChainItems.Length - 1; chainItemIndex >= 0; chainItemIndex--)
                    {
                        string chainItem = callChainItems[chainItemIndex];
                        if (chainItem == "anonymous function")
                        {
                            chainItem = "Anonymous function";
                        }

                        JsErrorHelpers.WriteErrorLocationLine(stackBuilder, chainItem, string.Empty, 0, 0);
                        if (chainItemIndex > 0)
                        {
                            stackBuilder.AppendLine();
                        }
                    }

                    callStack = stackBuilder.ToString();
                    stringBuilderPool.Return(stackBuilder);
                }

                type    = JsErrorType.Range;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStack);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRecursionException);
            }
            else if (originalRuntimeException is OriginalStatementsCountOverflowException)
            {
                type    = JsErrorType.Range;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }
            else if (originalRuntimeException is OriginalExecutionCanceledException)
            {
                _cancellationTokenSource = new CancellationTokenSource();
                _cancellationConstraint.Reset(_cancellationTokenSource.Token);

                type        = JsErrorType.Common;
                message     = CoreStrings.Runtime_ScriptInterrupted;
                description = message;

                wrapperRuntimeException = new WrapperInterruptedException(message,
                                                                          EngineName, EngineVersion, originalRuntimeException);
            }
            else
            {
                type    = JsErrorType.Common;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }

            wrapperRuntimeException.Description  = description;
            wrapperRuntimeException.Type         = type;
            wrapperRuntimeException.DocumentName = documentName;
            wrapperRuntimeException.LineNumber   = lineNumber;
            wrapperRuntimeException.ColumnNumber = columnNumber;
            wrapperRuntimeException.CallStack    = callStack;

            return(wrapperRuntimeException);
        }
Example #11
0
        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);
        }
Example #12
0
        private WrapperRuntimeException WrapRuntimeException(OriginalRuntimeException originalRuntimeException)
        {
            WrapperRuntimeException wrapperRuntimeException;
            string message = originalRuntimeException.Message;

            if (string.IsNullOrWhiteSpace(message))
            {
                message = "An unknown error occurred";
            }
            string description  = message;
            string type         = string.Empty;
            string documentName = string.Empty;
            int    lineNumber   = 0;
            int    columnNumber = 0;
            string callStack    = string.Empty;

            if (originalRuntimeException is OriginalJavaScriptException)
            {
                var originalJavaScriptException = (OriginalJavaScriptException)originalRuntimeException;
                documentName = originalJavaScriptException.Location.Source;
                lineNumber   = originalJavaScriptException.LineNumber;
                columnNumber = originalJavaScriptException.Column + 1;

                ErrorLocationItem[] callStackItems = JintJsErrorHelpers.ParseErrorLocation(
                    originalJavaScriptException.StackTrace);
                if (callStackItems.Length > 0)
                {
                    JintJsErrorHelpers.FixErrorLocationItems(callStackItems);
                    callStack = JsErrorHelpers.StringifyErrorLocationItems(callStackItems, true);
                }

                OriginalValue errorValue = originalJavaScriptException.Error;
                if (errorValue.IsObject())
                {
                    OriginalObjectInstance errorObject = errorValue.AsObject();

                    OriginalValue namePropertyValue = errorObject.Get("name");
                    if (namePropertyValue.IsString())
                    {
                        type = namePropertyValue.AsString();
                    }
                }

                if (string.IsNullOrEmpty(type))
                {
                    type = JsErrorType.Common;
                }

                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStack);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalJavaScriptException);
            }
            else if (originalRuntimeException is OriginalMemoryLimitExceededException)
            {
                type    = JsErrorType.Common;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }
            else if (originalRuntimeException is OriginalRecursionDepthOverflowException)
            {
                var originalRecursionException = (OriginalRecursionDepthOverflowException)originalRuntimeException;
                callStack = JintJsErrorHelpers.ConvertCallChainToStack(originalRecursionException.CallChain);
                type      = JsErrorType.Range;
                message   = JsErrorHelpers.GenerateScriptErrorMessage(type, description, callStack);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRecursionException);
            }
            else if (originalRuntimeException is OriginalStatementsCountOverflowException)
            {
                type    = JsErrorType.Range;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }
            else if (originalRuntimeException is OriginalExecutionCanceledException)
            {
                _cancellationTokenSource = new CancellationTokenSource();
                _cancellationConstraint.Reset(_cancellationTokenSource.Token);

                type        = JsErrorType.Common;
                message     = CoreStrings.Runtime_ScriptInterrupted;
                description = message;

                wrapperRuntimeException = new WrapperInterruptedException(message,
                                                                          EngineName, EngineVersion, originalRuntimeException);
            }
            else
            {
                type    = JsErrorType.Common;
                message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, string.Empty);

                wrapperRuntimeException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                      originalRuntimeException);
            }

            wrapperRuntimeException.Description  = description;
            wrapperRuntimeException.Type         = type;
            wrapperRuntimeException.DocumentName = documentName;
            wrapperRuntimeException.LineNumber   = lineNumber;
            wrapperRuntimeException.ColumnNumber = columnNumber;
            wrapperRuntimeException.CallStack    = callStack;

            return(wrapperRuntimeException);
        }
        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);
        }