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
return valid Python Exceptions when js2python is applied to javascript errors #891
Conversation
It would be better to implement the `JsException` class in C to fix `JsException_AsJs`. Currently `JsException_AsJs` can cause a Python exception and I don't think it will be handled correctly. Also there is a serious issue: if the Javascript `Error` was created without `new`, this will crash the Pyodide runtime. I have no idea why, I guess I need to carefully investigate the difference between a `new Error()` and `Error()`.
e173e28
to
7b71e97
Compare
7b71e97
to
bd6ac4b
Compare
I think this is ready now @oeway. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
I have two questions related to this: 1) have you figured why it's crashing when using Error without new? 2) if we do a round trip from js to python then back to js, will we get the same error object?
Yes I was incorrectly using a C Python API causing undefined behavior. This particular symptom was bad luck. It is fixed.
Yes. See test here: |
ad90a07
to
0b934df
Compare
Great, thank you! |
@rth I think this is ready to be merged unless you have further concerns? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8f5056b
to
5dafd38
Compare
I also added an entry for from js import js_async_func
from pyodide import JsException
async def wrapper(arg):
try:
await js_async_func(arg)
except JsException:
raise ValueError(f"Unknown thunk {arg}") |
5dafd38
to
83eb754
Compare
This does not fix #636. To do that we should:
I can implement that as a separte PR after this is merged, or I could add it to this PR, whichever you prefer. Error conversion in the other direction is also not very good: currently if python code throws an error, it prints stuff to stdout directly, but it should instead be possible to catch the error in javascript. To get the errors printed to stdout, we should explicitly catch them and call some separate print function. |
I think the title "Raise Exceptions when js2python is applied to javascript errors" is misleading: f raises an error: def f():
raise Exception("hi") f returns an error: def f():
return Exception("hi") |
I had to shorten it as it was too long for a commit message. Didn't mean to rename the issue only the commit. I agree that's not ideal. |
Thanks! |
This is intended to resolve #888, necessary for PR #880 to work correctly.
Work in progress.
Currently there is a serious issue: if the Javascript
Error
was created withoutnew
, this will crash the Pyodide runtime. I have absolutely no idea why.