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

Error in event handlers when handling JSON is caught and discarded #437

Closed
gmrchk opened this issue Mar 9, 2020 · 4 comments
Closed

Error in event handlers when handling JSON is caught and discarded #437

gmrchk opened this issue Mar 9, 2020 · 4 comments

Comments

@gmrchk
Copy link

gmrchk commented Mar 9, 2020

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
Any error in event handlers when handling JSON is caught and discarded by the library. If the error repeats with the string version of the input, only that is displayed.
This try-catch is the cause.

emitJSON(eventName: string, data?: any): Dispatcher {

The library will catch any error in the event handler, and emit the event once again with a string. In case the handler contains any check for the format of input and doesn't run with the string input, it won't error at all.

In general, being benevolent about the format (try JSON, fallback to string) might not be an optimal solution, but that's just my opinion.

If the current behavior is a bug, please provide the steps to reproduce and
if possible a minimal demo of the problem via https://jsfiddle.net or similar.

Throw an error in any event handler. It will only throw an error in the second run here.

this.emit(eventName, data);

const handler = () => throw Error("Error that won't be displayed");
channel.bind(`api-${eventName}`, handler);

What is the expected behavior?
Try-catch block should only try to parse the JSON and another try-catch can be present for the actual call of the handler.

Which versions of Pusher, and which browsers / OS are affected by this issue?
Did this work in previous versions of Pusher? If so, which?

Latest, probably continuous problem.

leesio pushed a commit that referenced this issue Jul 6, 2020
As per #437.
If the handler throws an error, we catch it and discard it. We shouldn't
be doing anything with the caller's handlers.
@leesio
Copy link
Contributor

leesio commented Jul 6, 2020

@gmrchk thanks for raising this. This is definitely not good behaviour. If I've understood properly, I think #475 should resolve it. What do you think?

In general, being benevolent about the format (try JSON, fallback to string) might not be an optimal solution, but that's just my opinion.

I happen to share this opinion. Unfortunately it's a change that has a high chance of breaking lots of stuff so it would have to be in a major release.

@gmrchk
Copy link
Author

gmrchk commented Jul 6, 2020

Nice work @leesio. It should save many people tedious debugging by showing the actual error right away. It was very confusing to search for why is there even a string received instead of JSON.

I happen to share this opinion. Unfortunately it's a change that has a high chance of breaking lots of stuff so it would have to be in a major release.

Yep, definitely something to learn from in the future versions. Your fix addresses the main issue now. 👍

leesio pushed a commit that referenced this issue Jul 7, 2020
* Don't catch and discard errors thrown by handlers

As per #437.
If the handler throws an error, we catch it and discard it. We shouldn't
be doing anything with the caller's handlers.
@leesio
Copy link
Contributor

leesio commented Jul 31, 2020

Hey @gmrchk I just released v7.0.0 which includes the fix for this. If you could test it works as expected I'll close the issue 😄

@leesio
Copy link
Contributor

leesio commented Sep 23, 2020

I’m going to close this, as it should be resolved now. If I’m mistaken please reopen

@leesio leesio closed this as completed Sep 23, 2020
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

No branches or pull requests

2 participants