Closed
Description
This is a feature suggestion for adding an error handling API to Svelte components based on this remark.
There is currently no built-in way to handle errors in Svelte components. React 16 introduced the concept of Error Boundaries to standardise this:
// Error handling life cycle in React:
componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true });
// You can also log the error to an error reporting service
logErrorToMyService(error, info);
}
I imagine a similar concept would be a good addition to Svelte components. Maybe
declaratively, as event on element:
<MyComponent on:error='doSomethingWith(e)' />
imperatively, as component life cycle event:
oncreate() { /* ... */ },
onerror(e) { /* ... */ },
I would check if the error is handled inside the component - with the onerror
handler - first, and only if it isn't handled there, have it bubble up and trigger on:error
. If a developer wants to handle the error inside the component and also wants to have it bubble up, they can use this.fire('error', e)
within onerror
.