agracio / edge-js

Run .NET and Node.js code in-process on Windows, macOS, and Linux
MIT License
618 stars 93 forks source link

Throwing an exception in C# #171

Closed p1pah closed 5 months ago

p1pah commented 1 year ago

While trying to implement edge-js in our application we have been trying to apply the ability to throw an exception from C# and handling it on the JS side. Following the example from the readme.

`var clrFunc = edge.func(function () {/ async (dynamic input) => { throw new Exception("Sample exception"); } /});

clrFunc(null, function (error, result) { if (error) { console.log('Is Error?', error instanceof Error); console.log('-----------------'); console.log(util.inspect(error, showHidden=true, depth=99, colorize=false)); return; } });`

We see the application blows up on the C# side and the error never gets back to the JS side, our terminal outputs:

undefined:1 (function (f, ctx) { return function (d, cb) { return f(d, cb, ctx); }; }) ^ Error: Sample exception at :1:55 at Object. (C:\Users\bgh\Downloads\VeryTemp12_29_12_32\main.js:85:1) at Module._compile (node:internal/modules/cjs/loader:1155:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10) at Module.load (node:internal/modules/cjs/loader:1033:32) at Function.Module._load (node:internal/modules/cjs/loader:868:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:22:47 {

Do we have to configure something in order to correctly handle this or is there a bug with the latest release? We are on version 19.3.0

agracio commented 1 year ago

Exceptions should be handled using try{} catch(error) {} . I have updated https://github.com/agracio/edge-js-quick-start with example.

p1pah commented 1 year ago

Thanks @agracio !

p1pah commented 1 year ago

One final question when talking about errors in this library.... when checking if an error is returned from edge-js e.g.

clrFunc(null, function (error, result) { if (error) { console.log('Is Error?', error instanceof Error); console.log('-----------------'); console.log(util.inspect(error, showHidden=true, depth=99, colorize=false)); return; } });

When would that actually happen? When the library itself returns an error object rather than a result object?