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);
        }
Ejemplo n.º 2
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);
        }