caoccao / Javet

Javet is Java + V8 (JAVa + V + EighT). It is an awesome way of embedding Node.js and V8 in Java.
https://www.caoccao.com/Javet/
Apache License 2.0
698 stars 68 forks source link

Engine error #345

Open AsyncProxy opened 4 months ago

AsyncProxy commented 4 months ago

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
AsyncProxy commented 4 months ago

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.

caoccao commented 4 months ago

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());
}
AsyncProxy commented 4 months ago
uncaughtException

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

AsyncProxy commented 4 months ago

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.

TapiocaFox commented 3 weeks ago

@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.