Error thrown from server action is not displayed on client side
Answered
Cape lion posted this in #help-forum
Cape lionOP
Hello,
I am doing a server side validation for duplicates using zod and throwing an error from action if the safeparse return success as false.
In this case I am throwing an error and catching the error at client to display the problem.
This is giving me an error -
An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.
I am doing a server side validation for duplicates using zod and throwing an error from action if the safeparse return success as false.
In this case I am throwing an error and catching the error at client to display the problem.
This is giving me an error -
An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.
Answered by joulev
if a server action throws, nextjs and react treat it as an unexpected error (a "devs messed up" situation rather than a "user messed up" situation). if the error is expected, catch it and return an object instead
6 Replies
@Cape lion Hello,
I am doing a server side validation for duplicates using zod and throwing an error from action if the safeparse return success as false.
In this case I am throwing an error and catching the error at client to display the problem.
This is giving me an error -
An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.
if a server action throws, nextjs and react treat it as an unexpected error (a "devs messed up" situation rather than a "user messed up" situation). if the error is expected, catch it and return an object instead
Answer
since nextjs and react treat it as unexpected, as a security precaution the error message is not sent to the client, instead only a generic message "something went wrong" is sent
Cape lionOP
@joulev Resolved it by returning an object with a friendly message. Thank you
McNab dog
How can i handle it in error.ts?
I would like to show a UI based on error.message.
@Cape lion <@484037068239142956> Resolved it by returning an object with a friendly message. Thank you
McNab dog
Also how do i do this when i need to return a promise.reject ?