Allow server actions to return a different HTTP status #49302
Replies: 10 comments 3 replies
-
any news on this @rclmenezes |
Beta Was this translation helpful? Give feedback.
-
I miss something like this too. Also the last time I tried on next 13.4 if I threw inside a server action locally would be fine but in production mode it would replace the message from the Error with a default one (Need to see if this is still a thing in 13.6) |
Beta Was this translation helpful? Give feedback.
-
Would be nice to get a response on this |
Beta Was this translation helpful? Give feedback.
-
Sveltekit handles it pretty nicely, and it would be amazing to have in Next.js: https://kit.svelte.dev/docs/errors. Something that might be good with a proposal like this is the |
Beta Was this translation helpful? Give feedback.
-
I would really love to have something like this. |
Beta Was this translation helpful? Give feedback.
-
:D Still waiting any one suggestion |
Beta Was this translation helpful? Give feedback.
-
I'm hoping for something similar, need to send a 401 not sure how to do it using server actions, don't wanna spin up an API route just for this |
Beta Was this translation helpful? Give feedback.
-
is this the reason why it's impossible?
so that "single server routrip" contains both server action result and UI updates; so can't have status code (or custom http header) |
Beta Was this translation helpful? Give feedback.
-
Still not possible today on Next@14.2.7 :( I resorted to |
Beta Was this translation helpful? Give feedback.
-
Still the same :) ? |
Beta Was this translation helpful? Give feedback.
-
Goal
Server actions should be able to return different HTTP statuses, like a 400 or 401.
Background
Currently, server actions return an HTTP 200 on success and an HTTP 500 on error.
They can also return a 301 via the
redirect
function and a404
via the notFound` function.However, it would be nice to support other status codes:
Returning an Http500 feels wrong in these scenarios. I want to be able to monitor my 40X responses.
Also, many people automate alerting on 500s; I don't want to get a Sentry alert whenever a script kiddie horses around with my endpoints.
Proposal
Two possible solutions:
These new functions could work like
redirect
ornotFound
:notAuthorized
would return a NEXT_NOT_AUTHORIZED errorbadRequest
would return a NEXT_BAD_REQUEST errorThis is probably the easiest solution. But what if we wanted to return something weird like a 402 or a 418? Are we going to make a function for each one? Ew.
We can implement this by returning a NEXT_CUSTOM_HTTP error object, and store
statusCode
as an attribute, andbody
as another attribute.(While I have this soapbox, I dislike that the
redirect
andnotFound
functions throw the error for you. I'd much rather explicitly callthrow redirect('/blah')
instead ofredirect('/blah')
.)Beta Was this translation helpful? Give feedback.
All reactions