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);
        }
Exemple #2
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);
        }