Javet icon indicating copy to clipboard operation
Javet copied to clipboard

Engine error

Open Vectorted opened this issue 1 year ago • 4 comments

Errors in APIs such as setTimeout and setnterval cannot be caught, and errors will be thrown directly.

Error

setTimeout(() => {throw new Error('')})
                                                                                                                      ^
                                                                                                    
                                                                                                    Error
                                                                                                        at Timeout._onTimeout (c.js:5:25)
                                                                                                        at listOnTimeout (node:internal/timers:573:17)
                                                                                                        at process.processTimers (node:internal/timers:514:7)
--------- beginning of crash
2024-05-30 19:58:41.669  3792-3828  nodejs                  com.proxy.plus                       E  
                                                                                                    Node.js v20.12.2
```js

Vectorted avatar May 30 '24 12:05 Vectorted

Errors in APIs such as setTimeout and setnterval cannot be caught, and errors will be thrown directly.

Error

setTimeout(() => {throw new Error('')})
                                                                                                                      ^
                                                                                                    
                                                                                                    Error
                                                                                                        at Timeout._onTimeout (c.js:5:25)
                                                                                                        at listOnTimeout (node:internal/timers:573:17)
                                                                                                        at process.processTimers (node:internal/timers:514:7)
--------- beginning of crash
2024-05-30 19:58:41.669  3792-3828  nodejs                  com.proxy.plus                       E  
                                                                                                    Node.js v20.12.2
```js

Any API with blocking, Java can't stop the engine from reporting errors.

Vectorted avatar May 30 '24 13:05 Vectorted

This is a Node.js feature that you missed. Basically, application is supposed to listen the event uncaughtException, otherwise, Node.js crashes.

Please review the following test code.

nodeRuntime.getExecutor("let a = [];\n" +
        "process.on('uncaughtException', (error, origin) => {\n" +
        "  a.push(origin, error);\n" +
        "});").executeVoid();
nodeRuntime.getExecutor("setTimeout(() => { throw new Error('Error'); }, 0);").executeVoid();
nodeRuntime.await();
assertEquals("uncaughtException", nodeRuntime.getExecutor("a[0]").executeString());
try (V8ValueError v8ValueError = nodeRuntime.getExecutor("a[1]").execute()) {
    assertEquals("Error", v8ValueError.getMessage());
}

caoccao avatar May 31 '24 03:05 caoccao

uncaughtException

Sorry so much, this is my problem, I'm using unhandledRejection instead of uncaughtException, much appreciated!

Vectorted avatar May 31 '24 04:05 Vectorted

This is a Node.js feature that you missed. Basically, application is supposed to listen the event , otherwise, Node.js crashes.uncaughtException

Please review the following test code.

nodeRuntime.getExecutor("let a = [];\n" +
        "process.on('uncaughtException', (error, origin) => {\n" +
        "  a.push(origin, error);\n" +
        "});").executeVoid();
nodeRuntime.getExecutor("setTimeout(() => { throw new Error('Error'); }, 0);").executeVoid();
nodeRuntime.await();
assertEquals("uncaughtException", nodeRuntime.getExecutor("a[0]").executeString());
try (V8ValueError v8ValueError = nodeRuntime.getExecutor("a[1]").execute()) {
    assertEquals("Error", v8ValueError.getMessage());
}

And I replied that the reason why 3.1.2 can't start is because of setResourceName, if the Node.js engine uses this api, the engine won't execute.

Vectorted avatar May 31 '24 04:05 Vectorted

@caoccao Is it possible to make the exception catchable in Java? So the program can to things correspondingly like close resources, cancel jobs etc. And perhaps just to prevent it crash the system entirely.

For example in this scenario:

nodeRuntime.getExecutor(script).setModule(true).executeVoid()

I figured it is setModule(true) that causes the problem.

TapiocaFox avatar Sep 10 '24 19:09 TapiocaFox