-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Catch possible errors in listeners to avoid breaking execution #14149
Catch possible errors in listeners to avoid breaking execution #14149
Conversation
I'm conflicted on this. On one hand, we have some precedent in preventing plugins from crashing the editor with block-level and application-level error boundaries. On the other hand, I'm not sure we want or need to have I guess it depends:
|
Thanks for the comment @aduth. These errors do not crash the editor, but may crash others code. I don't have enough expertise to know other ways to isolate the listener functions. I've been exploring this for a while and didn't find a better solution than the try/catch approach. Again, not sure if other extension points also need this kind of solutions, but it was really annoying find this problem I mention in the PR the hard way (one of our users complained about our plugin not working when in fact the error was another plugin breaking the subscription process by subscribing a listener function with errors...). As a plugin developer, if an apparently innocuous solution like the try/catch presented here may help avoid these kind of problems caused by unrespectful developers, I'd totally go for it. On the other hand, I understand your concerns. |
It's an interesting problem. I'd agree that ultimately the current situation leads to scenarios like the one you describe, which are very difficult to debug. Since the default behavior of an uncaught thrown error is to halt iteration, I'd also agree that a The last question is more of a broad point, but it ties back to my previous comment on consistent approaches to where and how we handle errors in the application. As it impacts this pull request, I might suggest considering one the following actions:
I might also consider raising this as a discussion point for the upcoming JavaScript meeting tomorrow. |
Thanks for the comments @aduth. I'm not sure at all about what a browser does with an uncaught error, but when I was thinking about this problem and trying to find solutions to it I was reading several sources on how to handle errors and almost everybody was using Then I got to the same approach you linked 'holding' errors and throwing them afterwards. However, I found the I believe the feedback from the JavaScript meeting later today may help here. |
Hi There! I've triaging PRs today and it seems this one is stale. how was the discussion in #core-js |
Description
Imagine we have two plugins (or code fragments) that subscribe to changes in the block editor. The first listener function subscribed is:
and the second listener function subscribed is:
An error thrown in the first listener will prevent the execution of the second listener when a change in the editor triggers the execution of all listeners subscribed to changes. This may cause careless programmers to break the execution of others' code.
The proposed solution here surrounds the execution of each listener with a
try/catch
statement that avoids the execution break.Some may say that code inside listeners should be developed in a way to avoid throwing errors. But adding the
try/catch
we make sure this will not happen.How has this been tested?
Just copy the previous pair of listeners, paste them in the web browser JS console when editing a post in the block editor, and make some change to trigger their execution.
Types of changes
I added a
try/catch
statement with aconsole.log
printing the error.Checklist: