I cannot figure out how to catch and handle unhandled exceptions globally on the client side in SvelteKit. I have created a very quick demo app below to show what I am seeing:
https://github.com/Jabronironi/SvelteKitErrorHandleExample
What I have noticed is:
- SSR On and Server Error = Routed to Error Page
- SSR On and Client Error = Requested page rendered on the server successfully, Requested page logs error in console when trying to render locally, and displays the prerendered server page to user.
- SSR Off and Server OR Client Error = White Page of Death, error is logged in console.
My goal is to catch errors that occur client side (not on the server) smoothly and globally to improve client experience. My current solution is creating an error component and putting it on every page and wrapping most of my logic in try/catch that utilize the component when an error occurs. The issue with this approach is it is repetitive, and not truly global error handling. It is better than nothing, but I hate it. Has anyone else solved this in a better way? Is it possible for the framework to help solve this better?
I have also created a branch that shows one attempt at handling errors on client side better, but I hate it as it requires actually handling them on pages to pass up the data to the layout, and it actually doesn't catch all client errors. Regardless, you can see that here:
https://github.com/Jabronironi/SvelteKitErrorHandleExample/tree/CatchAndHandleInLayoutThroughStore