Exemple #1
0
        public void JsFunctionsThatThrowCanBeCaught()
        {
            var script = @"
export default () => { throw new Error('That is quite illogical, captain.'); };
";

            using (var rt = BaristaRuntimeFactory.CreateRuntime())
            {
                using (var ctx = rt.CreateContext())
                {
                    using (ctx.Scope())
                    {
                        var fn = ctx.EvaluateModule <JsFunction>(script);

                        JsScriptException ex = null;
                        try
                        {
                            var result = fn.Call();
                        }
                        catch (JsScriptException invokeException)
                        {
                            ex = invokeException;
                        }

                        Assert.NotNull(ex);
                        Assert.Equal("That is quite illogical, captain.", ex.Message);
                    }
                }
            }
        }
Exemple #2
0
        public void JsNativeFunctionsThatThrowCanBeHandled()
        {
            using (var rt = BaristaRuntimeFactory.CreateRuntime())
            {
                using (var ctx = rt.CreateContext())
                {
                    using (ctx.Scope())
                    {
                        var fnBoom = ctx.CreateFunction(new Action(() =>
                        {
                            throw new InvalidOperationException("A hole has been torn in the universe.");
                        }));
                        Assert.NotNull(fnBoom);

                        JsScriptException ex = null;
                        try
                        {
                            var result = fnBoom.Call();
                        }
                        catch (JsScriptException invokeException)
                        {
                            ex = invokeException;
                        }

                        Assert.NotNull(ex);
                        Assert.Equal("A hole has been torn in the universe.", ex.Message);
                    }
                }
            }
        }
 /// <summary>
 /// Writes a detailed error message to the buffer
 /// </summary>
 /// <param name="buffer">Instance of <see cref="StringBuilder"/></param>
 /// <param name="jsScriptException">JS script exception</param>
 private static void WriteScriptErrorDetails(StringBuilder buffer,
                                             JsScriptException jsScriptException)
 {
     if (!string.IsNullOrWhiteSpace(jsScriptException.Type))
     {
         buffer.AppendFormatLine("{0}: {1}", Strings.ErrorDetails_Type,
                                 jsScriptException.Type);
     }
     if (!string.IsNullOrWhiteSpace(jsScriptException.DocumentName))
     {
         buffer.AppendFormatLine("{0}: {1}", Strings.ErrorDetails_DocumentName,
                                 jsScriptException.DocumentName);
     }
     if (jsScriptException.LineNumber > 0)
     {
         buffer.AppendFormatLine("{0}: {1}", Strings.ErrorDetails_LineNumber,
                                 jsScriptException.LineNumber.ToString(CultureInfo.InvariantCulture));
     }
     if (jsScriptException.ColumnNumber > 0)
     {
         buffer.AppendFormatLine("{0}: {1}", Strings.ErrorDetails_ColumnNumber,
                                 jsScriptException.ColumnNumber.ToString(CultureInfo.InvariantCulture));
     }
     if (!string.IsNullOrWhiteSpace(jsScriptException.SourceFragment))
     {
         buffer.AppendFormatLine("{0}: {1}", Strings.ErrorDetails_SourceFragment,
                                 jsScriptException.SourceFragment);
     }
 }
        /// <summary>
        /// Generates a detailed error message
        /// </summary>
        /// <param name="jsScriptException">JS script exception</param>
        /// <param name="omitMessage">Flag for whether to omit message</param>
        /// <returns>Detailed error message</returns>
        public static string GenerateErrorDetails(JsScriptException jsScriptException,
                                                  bool omitMessage = false)
        {
            if (jsScriptException == null)
            {
                throw new ArgumentNullException(nameof(jsScriptException));
            }

            var           stringBuilderPool = StringBuilderPool.Shared;
            StringBuilder detailsBuilder    = stringBuilderPool.Rent();

            WriteCommonErrorDetails(detailsBuilder, jsScriptException, omitMessage);
            WriteScriptErrorDetails(detailsBuilder, jsScriptException);

            var jsRuntimeException = jsScriptException as JsRuntimeException;

            if (jsRuntimeException != null)
            {
                WriteRuntimeErrorDetails(detailsBuilder, jsRuntimeException);
            }

            detailsBuilder.TrimEnd();

            string errorDetails = detailsBuilder.ToString();

            stringBuilderPool.Return(detailsBuilder);

            return(errorDetails);
        }
 public static string Format(JsScriptException jsScriptException)
 {
     return(GenerateErrorDetails(jsScriptException));
 }