Server action error message now being sent to the client in production
Answered
British Shorthair posted this in #help-forum
British ShorthairOP
So when I am throwing an error from my server action, I am obviously passing an error message. In my development server, the client is catching the exact error message from the server action. But in production, it's a weird message that's far from the message I have thrown in the error object.
The message in the production is always: 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.
No matter what message I throw within the Error instance.
Here's an example code:
I need to catch the exact error message thrown from the error in the server action in the client. Please point me to what I am doing wrong. I thought about shifting to objects (Like
The message in the production is always: 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.
No matter what message I throw within the Error instance.
Here's an example code:
"use server";
export async function deliverOrder() {
const error = Math.random() > 0.5;
if (error) {
throw new Error("You got an error!");
}
return "Nope";
}
I need to catch the exact error message thrown from the error in the server action in the client. Please point me to what I am doing wrong. I thought about shifting to objects (Like
return { type: "error/response", message: "..." }
), but it's a lot of work to do (in the case of maintaining type safety).Answered by Plague
If you want to return a message to the client when an error happens return an actual serializable piece of data like
return { error: "Something wen wrong" }
throwing an error in a server environment will never expose that message to the client because it could possible contain sensitive information.1 Reply
If you want to return a message to the client when an error happens return an actual serializable piece of data like
return { error: "Something wen wrong" }
throwing an error in a server environment will never expose that message to the client because it could possible contain sensitive information.Answer