javascriptc#jint

JINT-How to catch exception in CatchClrExceptions in JINT?


I want to catch the clr exceptions thrown from javascript. Below things i have tried.

var registerScript = new Engine(c => c.AllowClr(typeof(Manager).Assembly ).CatchClrExceptions(ExceptionHandler(new Exception("Exception")) )).Execute("javascriptCode").GetValue("JavascriptFunction");

public static Predicate<Exception> ExceptionHandler(Exception ex)
{
      throw new Exception(ex.Message);
}

But i want i like this,

 var registerScript = new Engine(c => c.AllowClr(typeof(Manager).Assembly ).CatchClrExceptions(e=>ExceptionHandler(new Exception(e.Message)) )).Execute("javascriptCode").GetValue("JavascriptFunction");

i.e, i want to catch the exception from javascript and get that exception message.

Please help on this.


Solution

  • CatchClrExceptions allows you to catch exceptions that stem from an assembly and direct weather the Jint engine should pass the exception to the JS code, or weather to throw it as a .NET exception.

    namespace ConsoleApp5
    {
        public class Program
        {
            public static void Helper(string msg)
            {
                throw new Exception(msg);
            }
            static void Main(string[] args)
            {
                var registerScript = new Engine(c => c
                    .AllowClr(typeof(Program).Assembly)
                    // Allow exceptions from this assembly to surface as JS exceptions only if the message is foo
                    .CatchClrExceptions(ex => ex.Message == "foo")
                )
                .Execute(@"function throwException(){ 
                        try { 
                            var ConsoleApp5 = importNamespace('ConsoleApp5');
                            ConsoleApp5.Program.Helper('foo'); 
                            // ConsoleApp5.Program.Helper('goo'); // This will fail when calling execute becase the predicate returns false 
                            return ''; 
                        }  
                        catch(e) { 
                            return e; 
                        } 
                };
                var f = throwException();")
                .GetValue("f");
            }
        }
    }
    

    The Predicate passed to CatchClrExceptions should return true/false. It will receive the CLR exception that was thrown.

    There appears to be no way to be notified on handled exceptions with the Jint runtime. To catch parser exception (i.e. invalid JS code) you can surround the Execute with a regular try..catch(ParserException ex) { .. } this will catch any parse exceptions. For runtime exceptions you can also catch JavaScriptException which will be thrown on unhandled exceptions on execution.

    var engine = new Engine(c => c
        .DebugMode()
        .AllowClr(typeof(Program).Assembly)
    );
    
    engine.Step += Engine_Step;
    
    try
    {
        var r = engine
        .Execute(@"function throwException(){ 
            // undefined.test(); // This will cause a runtime exception
            // fun ction test () { } // This will cause a parser exception
            try { 
                throw 'Handled exception'; // No notification on this exception it is handled in JS
                return ''; 
            }  
            catch(e) { 
                return e; 
            } 
    };
    var f = throwException();")
        .GetValue("f");
    }
    catch (ParserException pEx)
    {
        Console.WriteLine("Parser Exception " + pEx.Message);
    }
    catch (JavaScriptException rEx)
    {
        Console.WriteLine("Runtime Exception " + rEx.Message);
    }