Next.js Discord

Discord Forum

server action error handling

Answered
Ray posted this in #help-forum
Open in Discord
Avatar
RayOP
According to the doc, we can return a object to display error on client. However, when I try to return redirect after the form process successful, the client recieve NEXT_REDIRECT and not redirecting....
Answered by Plague
I know exactly what's going on, it has nothing to do with Server Actions and everything to do with the redirect function.

You CANNOT wrap redirect in a try-catch block because the way redirect works it throw a NEXT_REDIRECT error in order to terminate rendering of the route segment in which it was thrown.

See docs: https://nextjs.org/docs/app/api-reference/functions/redirect#example
View full answer

30 Replies

Avatar
Oak apple gall wasp
there are 2 redirects avaialble, server and client based on the environment you are you need to pick appropiate one
Avatar
RayOP
Im using the one from next/navigation on the server action, is it correct?
Avatar
Oak apple gall wasp
it depends how you are using server action with redirect, could u show screenshot of ur codebase
Avatar
RayOP
Image
here you go
Avatar
Oak apple gall wasp
this is action right?
Avatar
RayOP
yes
Avatar
Oak apple gall wasp
maybe what u could do is that in ur component whenever u are using this function await it and then redirect from your component itself
Avatar
RayOP
you mean using client side redirect with useRouter hook, right?
Avatar
Oak apple gall wasp
for example

SignUp.tsx
<form action={ async () => {
  await signup()
  redirect('/')

}}>
...some staff
</form>
yeah
but best would be if you update ur next.js version to latest sinec server actions are stable now
and it shouldn't have some weird issues
Avatar
RayOP
it is the latest one
Avatar
Oak apple gall wasp
well then maybe try this and let me know if this will work
like this
Image
Image
if you want to go fully on server side then useRouter isn't an option
Avatar
RayOP
well, Im using it with useFormState hook.
it just redirect even there is a error message returnered
Image
Avatar
RayOP
I don't think server action is stable yet lol. this kind of implementation is so smooth with remix
Avatar
I know exactly what's going on, it has nothing to do with Server Actions and everything to do with the redirect function.

You CANNOT wrap redirect in a try-catch block because the way redirect works it throw a NEXT_REDIRECT error in order to terminate rendering of the route segment in which it was thrown.

See docs: https://nextjs.org/docs/app/api-reference/functions/redirect#example
Answer
Avatar
RayOP
oh yeah
Avatar
This literally just happened to me when I was using a try-catch block just like you in a server action. I was really confused for about 5 mins when I read the docs
Avatar
RayOP
thanks, i got it now
Avatar
Oak apple gall wasp
btw rediect doesn't work in client component u should use useRouter
Avatar
it should work on client components, it works everywhere, but, yes I agree if you're routing on the client definitely should use useRouter
Image
redirect should only be used on the client IMO if you're routing the user from your application to a complete different website/app
Avatar
Oak apple gall wasp
Ahhh yes.. well I forgot the docs but I was intutively using redirect on server and useRouter on client
Avatar
Yeah same lol, until I re-read, I thought redirect was only viable on server.