Example #1
0
        /// <summary>
        /// Converts a call chain to stack
        /// </summary>
        /// <param name="callChain">Call chain</param>
        /// <returns>Call stack</returns>
        public static string ConvertCallChainToStack(string callChain)
        {
            string callStack = string.Empty;

            string[] callChainItems = 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 == OriginalAnonymousFunctionName)
                    {
                        chainItem = WrapperAnonymousFunctionName;
                    }

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

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

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