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

Can't catch JSON.parse() error? #253

Open
luke-waymark-sage opened this issue Sep 15, 2021 · 2 comments
Open

Can't catch JSON.parse() error? #253

luke-waymark-sage opened this issue Sep 15, 2021 · 2 comments

Comments

@luke-waymark-sage
Copy link
Contributor

If I set the mode to 'json', and python prints something that isn't JSON, then python-shell's use of JSON.parse() causes an error as one would expect. However, I can't seem to catch the error and it's just crashing my program.

I've tried wrapping either a PythonShell() object or a PythonShell.run() function in a try catch, using the function (err) callback, listing to an 'error' emit, but nothing can seem to catch this error.

For now I can set it to text mode and manually call JSON.parse() but this seems an unsatisfying solution.

@Almenon
Copy link
Collaborator

Almenon commented Oct 2, 2021

Yeah... this isn't great :|

The problem is that the parse method is called inside a pipe handler, so the context in which the error happens is isolated to that specific handler. The handler is called asynchronously and you can't catch errors in asynchronus code with a try-catch (see # 2).

So for now unfortunately your solution is the correct one.

The library could be improved by catching the error inside the handler and emitting a parseError event, so I'll categorize this as a enhancement idea.

@petem24
Copy link

petem24 commented Jan 25, 2024

Same issue here

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

No branches or pull requests

3 participants