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);
        }
Пример #2
0
        private static WrapperException WrapJsException(OriginalException originalException,
                                                        string defaultDocumentName = null)
        {
            WrapperException wrapperException;
            JsErrorCode      errorCode    = originalException.ErrorCode;
            string           description  = originalException.Message;
            string           message      = description;
            string           type         = string.Empty;
            string           documentName = defaultDocumentName ?? string.Empty;
            int    lineNumber             = 0;
            int    columnNumber           = 0;
            string callStack      = string.Empty;
            string sourceFragment = string.Empty;

            var originalScriptException = originalException as OriginalScriptException;

            if (originalScriptException != null)
            {
                JsValue metadataValue = originalScriptException.Metadata;

                if (metadataValue.IsValid)
                {
                    JsValue     errorValue     = metadataValue.GetProperty("exception");
                    JsValueType errorValueType = errorValue.ValueType;

                    if (errorValueType == JsValueType.Error ||
                        errorValueType == JsValueType.Object)
                    {
                        JsPropertyId innerErrorPropertyId = JsPropertyId.FromString("innerException");
                        if (errorValue.HasProperty(innerErrorPropertyId))
                        {
                            JsValue      innerErrorValue    = errorValue.GetProperty(innerErrorPropertyId);
                            JsPropertyId metadataPropertyId = JsPropertyId.FromString("metadata");

                            if (innerErrorValue.HasProperty(metadataPropertyId))
                            {
                                errorValue    = innerErrorValue;
                                metadataValue = innerErrorValue.GetProperty(metadataPropertyId);
                            }
                        }

                        JsValue messagePropertyValue = errorValue.GetProperty("message");
                        string  localDescription     = messagePropertyValue.ConvertToString().ToString();
                        if (!string.IsNullOrWhiteSpace(localDescription))
                        {
                            description = localDescription;
                        }

                        JsValue namePropertyValue = errorValue.GetProperty("name");
                        type = namePropertyValue.ValueType == JsValueType.String ?
                               namePropertyValue.ToString() : string.Empty;

                        JsPropertyId descriptionPropertyId = JsPropertyId.FromString("description");
                        if (errorValue.HasProperty(descriptionPropertyId))
                        {
                            JsValue descriptionPropertyValue = errorValue.GetProperty(descriptionPropertyId);
                            localDescription = descriptionPropertyValue.ConvertToString().ToString();
                            if (!string.IsNullOrWhiteSpace(localDescription))
                            {
                                description = localDescription;
                            }
                        }

                        if (type == JsErrorType.Syntax)
                        {
                            errorCode = JsErrorCode.ScriptCompile;
                        }
                        else
                        {
                            JsPropertyId numberPropertyId = JsPropertyId.FromString("number");
                            if (errorValue.HasProperty(numberPropertyId))
                            {
                                JsValue numberPropertyValue = errorValue.GetProperty(numberPropertyId);
                                int     errorNumber         = numberPropertyValue.ValueType == JsValueType.Number ?
                                                              numberPropertyValue.ToInt32() : 0;
                                errorCode = (JsErrorCode)errorNumber;
                            }
                        }

                        JsPropertyId urlPropertyId = JsPropertyId.FromString("url");
                        if (metadataValue.HasProperty(urlPropertyId))
                        {
                            JsValue urlPropertyValue = metadataValue.GetProperty(urlPropertyId);
                            string  url = urlPropertyValue.ValueType == JsValueType.String ?
                                          urlPropertyValue.ToString() : string.Empty;
                            if (url != "undefined")
                            {
                                documentName = url;
                            }
                        }

                        JsPropertyId linePropertyId = JsPropertyId.FromString("line");
                        if (metadataValue.HasProperty(linePropertyId))
                        {
                            JsValue linePropertyValue = metadataValue.GetProperty(linePropertyId);
                            lineNumber = linePropertyValue.ValueType == JsValueType.Number ?
                                         linePropertyValue.ToInt32() + 1 : 0;
                        }

                        JsPropertyId columnPropertyId = JsPropertyId.FromString("column");
                        if (metadataValue.HasProperty(columnPropertyId))
                        {
                            JsValue columnPropertyValue = metadataValue.GetProperty(columnPropertyId);
                            columnNumber = columnPropertyValue.ValueType == JsValueType.Number ?
                                           columnPropertyValue.ToInt32() + 1 : 0;
                        }

                        string       sourceLine       = string.Empty;
                        JsPropertyId sourcePropertyId = JsPropertyId.FromString("source");
                        if (metadataValue.HasProperty(sourcePropertyId))
                        {
                            JsValue sourcePropertyValue = metadataValue.GetProperty(sourcePropertyId);
                            sourceLine = sourcePropertyValue.ValueType == JsValueType.String ?
                                         sourcePropertyValue.ToString() : string.Empty;
                            sourceFragment = TextHelpers.GetTextFragmentFromLine(sourceLine, columnNumber);
                        }

                        JsPropertyId stackPropertyId = JsPropertyId.FromString("stack");
                        if (errorValue.HasProperty(stackPropertyId))
                        {
                            JsValue stackPropertyValue          = errorValue.GetProperty(stackPropertyId);
                            string  messageWithTypeAndCallStack = stackPropertyValue.ValueType == JsValueType.String ?
                                                                  stackPropertyValue.ToString() : string.Empty;
                            string messageWithType = errorValue.ConvertToString().ToString();
                            string rawCallStack    = messageWithTypeAndCallStack
                                                     .TrimStart(messageWithType)
                                                     .TrimStart("Error")
                                                     .TrimStart(new char[] { '\n', '\r' })
                            ;
                            string callStackWithSourceFragment = string.Empty;

                            ErrorLocationItem[] callStackItems = CoreErrorHelpers.ParseErrorLocation(rawCallStack);
                            if (callStackItems.Length > 0)
                            {
                                ErrorLocationItem firstCallStackItem = callStackItems[0];
                                firstCallStackItem.SourceFragment = sourceFragment;

                                documentName = firstCallStackItem.DocumentName;
                                lineNumber   = firstCallStackItem.LineNumber;
                                columnNumber = firstCallStackItem.ColumnNumber;
                                callStack    = CoreErrorHelpers.StringifyErrorLocationItems(callStackItems, true);
                                callStackWithSourceFragment = CoreErrorHelpers.StringifyErrorLocationItems(callStackItems);
                            }

                            message = CoreErrorHelpers.GenerateScriptErrorMessage(type, description,
                                                                                  callStackWithSourceFragment);
                        }
                        else
                        {
                            message = CoreErrorHelpers.GenerateScriptErrorMessage(type, description, documentName,
                                                                                  lineNumber, columnNumber, sourceFragment);
                        }
                    }
                    else if (errorValueType == JsValueType.String)
                    {
                        message     = errorValue.ToString();
                        description = message;
                    }
                    else
                    {
                        message     = errorValue.ConvertToString().ToString();
                        description = message;
                    }
                }

                WrapperScriptException wrapperScriptException;
                if (errorCode == JsErrorCode.ScriptCompile)
                {
                    wrapperScriptException = new WrapperCompilationException(message, EngineName, EngineVersion,
                                                                             originalScriptException);
                }
                else if (errorCode == JsErrorCode.ScriptTerminated)
                {
                    message     = CoreStrings.Runtime_ScriptInterrupted;
                    description = message;

                    wrapperScriptException = new WrapperInterruptedException(message,
                                                                             EngineName, EngineVersion, originalScriptException)
                    {
                        CallStack = callStack
                    };
                }
                else
                {
                    wrapperScriptException = new WrapperRuntimeException(message, EngineName, EngineVersion,
                                                                         originalScriptException)
                    {
                        CallStack = callStack
                    };
                }
                wrapperScriptException.Type           = type;
                wrapperScriptException.DocumentName   = documentName;
                wrapperScriptException.LineNumber     = lineNumber;
                wrapperScriptException.ColumnNumber   = columnNumber;
                wrapperScriptException.SourceFragment = sourceFragment;

                wrapperException = wrapperScriptException;
            }
            else
            {
                if (originalException is OriginalUsageException)
                {
                    wrapperException = new WrapperUsageException(message, EngineName, EngineVersion,
                                                                 originalException);
                }
                else if (originalException is OriginalEngineException)
                {
                    wrapperException = new WrapperEngineException(message, EngineName, EngineVersion,
                                                                  originalException);
                }
                else if (originalException is OriginalFatalException)
                {
                    wrapperException = new WrapperFatalException(message, EngineName, EngineVersion,
                                                                 originalException);
                }
                else
                {
                    wrapperException = new WrapperException(message, EngineName, EngineVersion,
                                                            originalException);
                }
            }

            wrapperException.Description = description;

            return(wrapperException);
        }
        private WrapperException WrapJsException(OriginalException originalException)
        {
            WrapperException wrapperException;

            var originalScriptException = originalException as OriginalScriptException;

            if (originalScriptException != null)
            {
                WrapperScriptException wrapperScriptException;

                var originalRuntimeException = originalScriptException as OriginalRuntimeException;
                if (originalRuntimeException != null)
                {
                    WrapperRuntimeException wrapperRuntimeException;
                    if (originalRuntimeException is OriginalInterruptedException)
                    {
                        wrapperRuntimeException = new WrapperInterruptedException(originalScriptException.Message,
                                                                                  EngineName, _engineVersion, originalRuntimeException);
                    }
                    else
                    {
                        wrapperRuntimeException = new WrapperRuntimeException(originalScriptException.Message,
                                                                              EngineName, _engineVersion, originalRuntimeException);
                    }
                    wrapperRuntimeException.CallStack = originalRuntimeException.CallStack;

                    wrapperScriptException = wrapperRuntimeException;
                }
                else if (originalScriptException is OriginalCompilationException)
                {
                    wrapperScriptException = new WrapperCompilationException(originalScriptException.Message,
                                                                             EngineName, _engineVersion, originalScriptException);
                }
                else
                {
                    wrapperScriptException = new WrapperScriptException(originalScriptException.Message,
                                                                        EngineName, _engineVersion, originalScriptException);
                }

                wrapperScriptException.Type           = originalScriptException.Type;
                wrapperScriptException.DocumentName   = originalScriptException.DocumentName;
                wrapperScriptException.LineNumber     = originalScriptException.LineNumber;
                wrapperScriptException.ColumnNumber   = originalScriptException.ColumnNumber;
                wrapperScriptException.SourceFragment = originalScriptException.SourceFragment;

                wrapperException = wrapperScriptException;
            }
            else
            {
                if (originalException is OriginalUsageException)
                {
                    wrapperException = new WrapperUsageException(originalException.Message,
                                                                 EngineName, _engineVersion, originalException);
                }
                else if (originalException is OriginalEngineException)
                {
                    if (originalException is OriginalEngineLoadException)
                    {
                        wrapperException = new WrapperEngineLoadException(originalException.Message,
                                                                          EngineName, _engineVersion, originalException);
                    }
                    else
                    {
                        wrapperException = new WrapperEngineException(originalException.Message,
                                                                      EngineName, _engineVersion, originalException);
                    }
                }
                else if (originalException is OriginalFatalException)
                {
                    wrapperException = new WrapperFatalException(originalException.Message,
                                                                 EngineName, _engineVersion, originalException);
                }
                else
                {
                    wrapperException = new WrapperException(originalException.Message,
                                                            EngineName, _engineVersion, originalException);
                }
            }

            wrapperException.Description = originalException.Description;

            return(wrapperException);
        }