-
Notifications
You must be signed in to change notification settings - Fork 428
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
Errors are not caught/handled within value changed & target connected callbacks #711
Comments
This appears to be an issue for target connected / disconnected callbacks also. |
Would the Stimulus team be up for a PR for this, it's causing many issues for our Stimulus adoption in both testing and creating robust controllers. |
For anyone writing unit tests, here's a way around it for now.
I use this in combination with a generic
This allows us to test valid/expected thrown errors within A similar workaround could probably be found for the value changed callbacks also. |
I have recently set up a controller where we want to ensure that a specific target exists and found what could be a bug in how the error handling works in Stimulus.
According to the documentation on error handling;
Problem
Errors in the
<some>valueChanged
callbacks are not being caught like all other errors and result in code that is hard to debug, log and test.Steps to reproduce (basic)
Steps to reproduce (full details)
Consider this controller code.
And this HTML.
Observe that there is no
toggle
target set in the HTML, accessing this in the controller will cause a caught error (as expected).Now, refactor this same controller to use value changed callbacks.
With the same HTML, we now get an uncaught error, this does not get handled by Stimulus, not logged as expected and also makes it very difficult to unit test this scenario. The message is there but there is no way for this to be caught by any code easily.
I can kind of work around this by doing a check of targets that we care about in the initalize callback (called before the first changed callback).
We still get the uncaught error though, but at least we get a caught one just before it. However, this is not going to help when unit testing this case as the process will still fail.
Potential solution
I think the issue is that the private method
invokeChangedCallback
does not pass the error tohandleError
. Instead it just throws it, but I cannot see any places where this method is called that catch this error, causing it to bubble up.stimulus/src/core/value_observer.ts
Lines 98 to 103 in 7b810ec
Maybe we can avoid this by passing the error to the
handleError
instead as this pattern is used by other code.Note: I have not tested this but just a guess.
The text was updated successfully, but these errors were encountered: