-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ThreadSafeFunction : The exception is threw in CPP, but not JS. #669
Comments
How would you like to handle the thrown errors in an async way? They cannot be caught with try...catch block. |
I mean to use the simple example above to show my problem. I do this becasue I have a CPP addon, which has a worker thread to callback some data and I need to transform these data from CPP to JS. When using Then I need to write some JS code to deal with these data. If I make some mistakes in JS code, the exception will be thrown in CPP, but not in JS. Is there a way to handle the exception in JS, but not in CPP? Thank you. |
The error was thrown into c++ (which is not quite accurate, the errors are still on JavaScript land, but c++ addon have to handle it or the process had to crash) because the one who called the throwing JavaScript function is the c++ addon. You may wrap the JavaScript callback to handle the exceptions early in JavaScript: function wrap(fn) {
return function wrapper(...args) {
try {
return fn.apply(this, args);
} catch (e) {
// do what you'd like to do with the error `e` here.
}
};
}
function callback(msg) {
console.log(msg);
let s = sssss
console.log(msg);
}
addon.asyncCallback(wrap(callback)); |
@legendecas Thanks for you reply. It works. |
Sorry to open this issue again. Using the wrap function, I can catch the exceptions early in JavaScript. But I still have an another question. When I use the CPP addon in electron, the but the Is there way to make the |
You can print the error with |
@legendecas Thanks for your reply. |
I'm using
ThreadSafeFunction
to implement callbacks from CPP to JS. If an exception occur in JavaScript, it will be threw in CPP, but not JS.The test project can be found here
Environment
SyncCallback Example(the right way)
let s = sssss
will throw an exceptionAsyncCallback using ThreadSafeFunction
let s = sssss
will throw an exceptionQuestion
When using ThreadSafeFunction, how to throw exceptions in JS like the SyncCallback Example?
The text was updated successfully, but these errors were encountered: