Error Handler
Answered
English Angora posted this in #help-forum
English AngoraOP
please tell me proper error handling in Nextjs
i want to handle errors comes from server can you suggest me i am confuse in these two approaches use server actions with error boundary or use api routes with NextResponse
i am using react hot toast also
i want to handle errors comes from server can you suggest me i am confuse in these two approaches use server actions with error boundary or use api routes with NextResponse
i am using react hot toast also
Answered by aardani
return { error: "message" }, something like that
then you can just grab the result in the client
then you can just grab the result in the client
useEffect(()=>{
const res = await serverAction()
console.log(res.error) // do something with error message
},[])25 Replies
Peterbald
Please show the code and error that you faced/...
English AngoraOP
i want to implement error handling like i am uploading image if image uploading failed i want to show error to user i am using currently server actions with client component
Peterbald
You can receive response from API.
English AngoraOP
i am using react hot toase library for showing notifications
i think Error boundary work on pages
Peterbald
yeah if you get the response from API you can call toast function
English AngoraOP
Error boundary works with react hot toast ?
Peterbald
ofcourse
English AngoraOP
where is api defined ?
Peterbald
/api/loginUser.ts api
As you know api is server action. You can send 200 or 400 or 401 response to client in API
And you can notify based on the response status
English AngoraOP
so there is no need of Error Boundary component
Peterbald
You can check Error Boundar in server action and send the only data and status to client
English AngoraOP
i think Error Boundary is not suitable with react hot toast
@English Angora please tell me proper error handling in Nextjs
i want to handle errors comes from server can you suggest me i am confuse in these two approaches use server actions with error boundary or use api routes with NextResponse
i am using react hot toast also
return { error: "message" }, something like that
then you can just grab the result in the client
then you can just grab the result in the client
useEffect(()=>{
const res = await serverAction()
console.log(res.error) // do something with error message
},[])Answer
use server actions with error boundary or use api routes with NextResponseSure you can use api routes with NextResponse but you still need to determine the result from the body of the fetch anyway
And just to note, server actions dont have error boundary.
English AngoraOP
@aardani i also want to send status code to client can i do like this return {error, statusCode}
English AngoraOP
@Peterbald @aardani thankyou friends for help
no problem, make sure to mark the post as answered