Confusing semantics of throw error
, return invalid
, throw redirect
#6693
-
I'm trying to migrate my SvelteKit projects to the latest version, and I'm confused by the semantics of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
1.
|
Beta Was this translation helpful? Give feedback.
-
I still wonder about using Generally, exceptions should only be used for truly exceptional situations. I think of exception pathways like a service road next to a highway. If we put regular traffic on the service road, we no longer have a service road. Also, it means |
Beta Was this translation helpful? Give feedback.
1.
invalid
This one's used for returning validation information in a form
action
. It's not used in anyload
functions.https://kit.svelte.dev/docs/form-actions#anatomy-of-an-action-validation-errors
2.
error
You'll want to use
throw error(400, "your reason")
for expected errors.For example,
401 Unauthorised
when a user fails to log in. You expect this error to happen.You can use these in a
load
function or in a+server
endpoint forGET
,POST
, etc.https://kit.svelte.dev/docs/load#errors
3.
redirect
Similar semantics as
error
.https://kit.svelte.dev/docs/load#redirects
4. Why
throw
instead ofreturn
?I'm not smart enough to be in the know, so I'll just make my own assumptions:
It allow…