The js exception caught by iOS JSC is incomplete

35 Views Asked by At

I'm using JSContext#exceptionHandler on iOS to catch JS exceptions, but the resulting stack seems to be incomplete.

The OC code:

   NSString * path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"];
    NSData * jsData = [[NSData alloc]initWithContentsOfFile:path];
    NSString * jsCode = [[NSString alloc]initWithData:jsData encoding:NSUTF8StringEncoding];
    self.jsContext = [[JSContext alloc]init];
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exception) {
        NSString *message = [NSString stringWithFormat:@"[exception msg]:%@", exception];
        JSValue *stackTrace = [exception valueForProperty:@"stack"];
        NSString *stackTraceStr = [stackTrace toString];
        NSLog(@"[JS Exception]:%@", message);
        NSLog(@"[JS Exception]:%@",stackTraceStr);
    };
    [self.jsContext evaluateScript:jsCode];

The test.js code:

(function(){
    throw new Error('test');
})();

stack of console output:

2023-04-14 17:17:26.636593+0800 iosApp[24509:6788200] [JS Exception]:[exception msg]:Error: test
2023-04-14 17:17:26.636686+0800 iosApp[24509:6788200] [JS Exception]:@
global code@

Is there any way to be able to output the full JS stack information?

0

There are 0 best solutions below