Allow server actions to return a different HTTP status #49302
Replies: 14 comments 16 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 [email protected] :( I resorted to |
Beta Was this translation helpful? Give feedback.
-
Still the same :) ? |
Beta Was this translation helpful? Give feedback.
-
need it too |
Beta Was this translation helpful? Give feedback.
-
I'd also like to be able to control the status code because I think password managers will see the 200 status code and think authentication was successful and ask to save the password. |
Beta Was this translation helpful? Give feedback.
-
Surprisingly, there is no official answer yet. Does anyone know if it is even on the roadmap? We had to get rid of server-side actions because of multiple issues and pure support (e.g., implementing CSRF protection for SSA looks horrible, or not being able to return custom HTTP status codes, etc.). We started to use APIs instead, maybe I'll try SSA again but only for form submission. |
Beta Was this translation helpful? Give feedback.
-
Related: there's been some exploration into
What could be helpful here: what are the product requirements you are trying to achieve with the status code? Is there a tool your customers are using which expect to see such code? Or something else? The more specific the better 😄 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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