Open brandonp-ais opened 1 year ago
This is part of a larger problem I am having.
Catching unexpected server errors in hooks.server is of no use, if not for logging purpose.
One is then forced to try/catch
or if/else
their way in every server side handler - which is very inefficient.
What I would find more efficient is for the global error catch - hooks.server - to be able to handle errors: maybe throw a nicer error catched by hooks.client that could for example redirect to nice error page, redirect to authentication...
Would this have wrong side effects?
So either is like that or I am missing something big :)
But how would one handle this scenario:
if is a 401/3 I Would like to send user to /auth
.
If is something else, I would handle it differently.
What I don't think is good is go try/catch server side code everywhere.
This is especially an issue with SPAs with no SSR. There is no way to globally handle errors with some error component.
From our own testing, the core issue is when code in the hook modules, specifically universal and client hook modules, are being loaded and executed.
We had an internal expectation that code in hook modules would load and execute before any other code, so we placed code that we wanted to be executed before anything else in those modules. We had assumed that, if supported hooks were going to function as expected just as the case in this issue, then they'd need to load and run first. However, this is not the case.
The fact that the hooks don't load before a +page
file is executed means the behavior of hooks is either incorrect or less useful than we would hope. The fact that code in hooks modules don't execute before any other code (+page
or otherwise, like in $lib
) may or may not be incorrect behavior, but how and when these hook modules load and execute should be better documented so that we may be better able to decide how we may make use of them.
Describe the bug
Unexpected errors should be passed to
hooks.client.js#handleError()
so they could be sent to Sentry etc.https://kit.svelte.dev/docs/hooks#shared-hooks-handleerror
But if a
new Error()
is thrown from top level of a+page.svelte
script tag, the app crashes and the hook is not invoked.I would expect that such an error, which is mimicking an unexpected error, should be picked up by the hook.
Reproduction
Stackblitz reproduction
Logs
System Info
Severity
serious, but I can work around it
Additional Information
No response