-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Turn error
and redirect
into commands, rather than functions returning a value we have to throw
#11144
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
Turn error
and redirect
into commands, rather than functions returning a value we have to throw
#11144
Comments
I like the |
I'm not sure that'll really be a problem in practice; this is how Next's similar directives work (at least, from a typing perspective) and I haven't seen any similar feedback from their users. Additionally -- is there any reason this feature has to work via error throwing other than that it was convenient? |
I think this is the core issue. My two cents -- not throwing is preferable to misdirecting the throw. Returning the value of Having to allow some throws makes it difficult to enhance and compose server actions, ex: logging middleware. JS provides limited pattern matching, so it's non-trivial for the developer to assert "Oh ok, I should re-throw this error and not log it." |
it's because of this: function load(event) {
do_some_auth_stuff_or_whatever(event);
// ...
} if we had to return a function load(event) {
const result = do_some_auth_stuff_or_whatever(event);
if (result) {
return result;
}
// ...
} but then what if it can return something other than a redirect/error, like some details about the user? you'd have to determine that that was the case... and before you know it your simple reusable helper is just a PITA to use.
@tcc-sejohnson and I were just talking about exposing helpers like this... function isHttpError(error: any, status?: number): error is HttpError {
return error instanceof HttpError && (status ? error.status === status : true);
} ...so that you can do this sort of thing: try {
whatever();
} catch (e) {
if (isHttpError(e, 404)) {
// ...
}
} |
Describe the problem
Throwing
error
andredirect
has always been a bit weird, and people have complained about it before, but it is necessary -- both of them are meant to stop the normal control flow and start something new, which is kinda whatthrow
does. Remembering that you have to throw them, though, is strange, and it doesn't feel like a great developer API. Thankfully, TypeScript gives us what we need to have the best of both worlds:A function with a return type of
never
, well, never returns. TypeScript treats it like a thrown error. In the following code:TypeScript would grey out the last line, knowing that it could never be reached. This is ultra-simple to implement, and would basically just mean changing the implementation of the current
redirect
anderror
functions like so:Describe the proposed solution
Both combined above.
Alternatives considered
Leaving it as-is. The main downside here is that users can't do something like:
but the only reason they can do this today is because we didn't think of the above before this feature was released -- the actual redirect and error classes are meant to be implementation details not exposed to the user.
Importance
nice to have
Additional Information
No response
The text was updated successfully, but these errors were encountered: