-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Unable to handle bound getter/setter errors #14892
Comments
according to the documentation, error boundaries do not work for event handlers. oninput is obviously is an event handler but even from https://svelte.dev/docs/svelte/svelte-boundary
typically, any such errors that fall outside of the svelte:boundary, one would handle via a try/catch block but if the provided example indeed your use case, you would display an error to the user. Either use form validation provided attributes https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Forms/Form_validation or use custom errors via Or, you can pass in the Or, don't need to use |
Thanks for the info @Leonidaz. I hadn't explored using Perhaps one could write a bind-able I think you might misunderstand me on the event handlers. In the example above, the event handler is not throwing an error, rather the setter is. Once the setter throws an error, the (unrelated) event handler ceases to fire, until the page is reloaded, but the signal still appears to work fine. If I had to ask a follow-up question, it would be "why?" From a DX standpoint, the runtime difference between: function sideEffect() {
console.log("side effect");
throw "banana";
} —and function sideEffect() {
console.log("side effect");
}
throw "banana"; —seems marginal, but (in practice) depends entirely on where |
The setter is fired because of Your The reason why the oninput (delegated) event still fires is because that's how browsers work, even if there is an error in a handler the delegated handler up the dom tree still fires. The events are asynchronous.
In the playground,
I'm not sure what errors you expect from the input, unless your code generates them.
Not sure what you mean here, it would be helpful to have an an actual example vs a hypothetical.
So, technically you can create a way to capture errors with boundaries but it's an ugly solution that you should never use because it's easily done with catching errors using traditional methods.
The other thing to remember with svelte:boundaries is that it destroys the component / elements and they have to be recreated again: via a snippet (not safe as it could lead to infinite loops) or via a reset button. |
That's a great explanation, thanks.
I think you answered my question, which was that logical errors (unless directly applicable to signals) don't trigger rendering errors, which was a misunderstanding on my part.
I took a crack at solving my problem here. It's not as clean, but it keeps the error handling inside the component, rather than the class. Please let me know if this is along the lines of what you were initially suggesting.
I wanted to clarify what you meant by this. I tried this here, but I wasn't able to get it to work as expected.
Maybe we aren't seeing the same thing. Recording.2025-01-05.133206.mp4What I am seeing is that |
Ah, the playground console sucks, I only use the browser's (chrome) dev tools and if you look there, your handler keeps firing.
This example works although the svelte:boundary doesn't do anything as far as the validation errors. I don't know your requirements but if you need to use a class for validation and throw errors then this seems like a valid approach. I would personally just collect errors into something reactive and then display them as error messages below inputs or by using setCustomValidity to get the native browser experience.
I really don't recommend it for your use case, as mentioned. I only mentioned it because you were trying to make the error boundary work. But, the only way it would work in your use case if you threw an error inside an effect. Here's a repl using your example notice how the component disappears after the svelte boundary catches it, as you need fallback content and/or reset button. |
That's one mystery solved.
For sure, I think input validation + either of our solutions works better for my specific case (thanks again). As error handling has seemed mostly unecessary in the Svelte ecosystem, I was aware that my use case is probably an anti-pattern. I'm just exploring every avenue to better my understanding of how errors are bubbling here, and of course for anyone else who finds themselves recreating this wheel. |
Describe the bug
I noticed when you throw an error via a setter, then bind an input to it, that error is not treated as a UI/rendering/effect error; using
+error.svelte
or<svelte:boundary>
to try and catch the error does not result in anything happening. Instead, the error is logged to the console as "uncaught", and everything continues like nothing went wrong.—except it doesn't; some related event handlers/effects silently fail.
While I can appreciate that a getter/setter error isn't strictly UI/rendering/effect related, leaving the app in a half-working/UB state is a poor solution.
Is there another pattern for an issue like this? What ought someone do to handle these errors gracefully?
Reproduction
https://svelte.dev/playground/88805747a51c4ae981d01fff06573370?version=5.16.1
Enter a number into the box, notice how the UI updates + the side effect in the console.
Now delete the number. The error should appear in the console. Subsequent inputs will reflect in the UI, but notice how the "side effect" no longer runs (until the page is reloaded).
Lastly, none of this is caught by the
<svelte:boundary>
.Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: