openpeeps / denim

Node 💖 Nim = Denim! Build powerful NodeJS / BunJS addons with Nim language via Node API (NAPI)
https://openpeeps.github.io/denim/
MIT License
42 stars 1 forks source link

Nim exceptions break the node process #9

Closed jfilby closed 1 year ago

jfilby commented 1 year ago

I noticed that when an exception is raised from the Nim code in the addon, the exception not displayed in the console and any future Nim calls don't work. The node process has to be restarted.

It might be useful for Denim to catch all exceptions and log them to the console in a harmless way.

georgelemon commented 1 year ago

Or, catch from Nim and throw it in JS. Anything else can be done from your JS code.

Example, trying to parse some invalid JSON using std/json. Exception message + stack trace:

myaddonWithErrors()
^

Error: input(1, 5) Error: { expected
/tests/myaddon.nim(30) wrapper$
/tests/myaddon.nim(13) testException
/.choosenim/toolchains/nim-1.6.14/lib/pure/json.nim(1035) parseJson
/.choosenim/toolchains/nim-1.6.14/lib/pure/json.nim(951) parseJson
/.choosenim/toolchains/nim-1.6.14/lib/pure/json.nim(916) parseJson
/.choosenim/toolchains/nim-1.6.14/lib/pure/parsejson.nim(515) raiseParseErr

    at Object.<anonymous> (/tests/js/myaddon.js:4:5)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  code: 'NimRuntime'
}
georgelemon commented 1 year ago

Starting with v0.1.7 it will be available in {.export_napi.} pragma

jfilby commented 1 year ago

That's really great! Thanks.