-
Notifications
You must be signed in to change notification settings - Fork 587
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
No way to throw exceptions from native code #17
Comments
Yes, this is something that needs work. Do you have any examples of what you're trying to achieve? |
FWIW, an example, schematically: js helper
Go errf
Example native binding
Full code: https://github.com/cznic/tmp/blob/14d211b7818be2225f12f80fa299f0176ae771cb/g/glue.go |
So what you're trying to do is throw a panic from the inner Go code to the other Go code? |
No, the machinery allows the javascript clients to catch errors reported by
|
Yes, I think a Go panic should be visible within a try { ... } catch { ... }, probably as a PanicError, with no extra work necessary. I also think a (special) piercing panic should be possible, something that can bypass JavaScript. |
My example is not about Go panicking. It's about Go errors reporting. The
|
This is partially addressed in e2e79bb If you panic() something of type otto.Value, then it will behave as if you did a "throw" in JavaScript. For example: |
This is further addressed in 83c56dd I've added a .Call method to Otto, which allows you to call arbitrary JavaScript and return the result. You can now do something like this in your Go code:
If you want, a possible convenience function is:
With these two, you can do:
And it'll throw that exception in the JavaScript environment like you would expect |
Looks quite good to me. Thanks! |
Be careful when doing a raw panic of a complex object:
The above may cause memory issues with the Go runtime, see #59 |
If |
In a
func(otto.FunctionCall) otto.Value
, there is no (documented) way to throw a JavaScript exception that the caller can see.The text was updated successfully, but these errors were encountered: