Skip to content
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

Override name field in custom Error classes #7

Merged
merged 1 commit into from
Oct 2, 2023

Conversation

suhdonghwi
Copy link
Contributor

Hello, first of all thank you much for your great open-source projects. I am working on a personal project which uses Pyodide and your libraries are truly helpful.

This PR adds overriding of name field to custom Error classes - InterruptError and NoChannelError.

When an error object is thrown from JavaScript to Python, it is converted into JsException object and its field values are lost (except for .name and .message). Therefore from Python side, it is impossible to tell which type of error is thrown.

I thought that replacing type field with name field is more idiomatic (and maybe this is what you intended originally?), however this would be a breaking change.

@alexmojaki
Copy link
Owner

Hello, first of all thank you much for your great open-source projects. I am working on a personal project which uses Pyodide and your libraries are truly helpful.

Thank you! ❤️

I thought that replacing type field with name field is more idiomatic (and maybe this is what you intended originally?)

I didn't actually realise that .name was a standard property on JS errors. I'm annoyed that JS just gives Error by default 😆

When an error object is thrown from JavaScript to Python, it is converted into JsException object and its field values are lost (except for .name and .message). Therefore from Python side, it is impossible to tell which type of error is thrown.

This is surprising to me. When I run this code in https://futurecoder.io/course/#ide:

import js

try:
    js.eval("""
class EE extends Error {
}
ee = new EE()
ee.type = "eee"
throw ee
""")
except Exception as e:
    print(e.type)  # eee

it seems to work. Maybe it's different for class variables? I can't see a convenient way to check right now. It might also be worth opening a Pyodide issue?

I also wonder if .constructor.name works and then even type isn't needed. But maybe it's not reliable when the code is minified.

Either way, I can't see any real harm here, so I'll merge it. Thank you!

@alexmojaki alexmojaki merged commit 737460d into alexmojaki:master Oct 2, 2023
1 check failed
@alexmojaki
Copy link
Owner

Published version 0.0.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants