Throwing an Error on server action make our production app error
Answered
American black bear posted this in #help-forum
American black bearOP
here is my code
but i got error "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 know the error cause of throwing an error on action, but how to make it work without calling customTryCatch on action?
//submitFormFn
const { errors, data } = await customTryCatch(
generateCareerProgressionAction,
fromData
);
//customTryCatch
export default async function customTryCatch<T, A>(
action: (values: A) => T,
values: A
): Promise<ServerActionResponse<Awaited<T>>> {
try {
const data = await action(values);
return {
data,
};
} catch (error: unknown) {
if (process.env.NODE_ENV === "development") {
console.error(error);
}
if (error instanceof ServerActionError || error instanceof Error) {
return {
errors: error,
};
}
if (error instanceof ZodError) {
return {
errors: error,
};
}
return {
errors: {
message: "Internal server error",
},
};
}
}
//action
export async function generateCareerProgressionAction(formData: FormData) {
throw new Error("Error")'
}but i got error "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 know the error cause of throwing an error on action, but how to make it work without calling customTryCatch on action?
Answered by B33fb0n3
You can either do this:
https://joulev.dev/blogs/throwing-expected-errors-in-react-server-actions
Or (what I like to use) use this:
https://next-safe-action.dev/
https://joulev.dev/blogs/throwing-expected-errors-in-react-server-actions
Or (what I like to use) use this:
https://next-safe-action.dev/
3 Replies
@American black bear here is my code
//submitFormFn
const { errors, data } = await customTryCatch(
generateCareerProgressionAction,
fromData
);
//customTryCatch
export default async function customTryCatch<T, A>(
action: (values: A) => T,
values: A
): Promise<ServerActionResponse<Awaited<T>>> {
try {
const data = await action(values);
return {
data,
};
} catch (error: unknown) {
if (process.env.NODE_ENV === "development") {
console.error(error);
}
if (error instanceof ServerActionError || error instanceof Error) {
return {
errors: error,
};
}
if (error instanceof ZodError) {
return {
errors: error,
};
}
return {
errors: {
message: "Internal server error",
},
};
}
}
//action
export async function generateCareerProgressionAction(formData: FormData) {
throw new Error("Error")'
}
but i got error "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 know the error cause of throwing an error on action, but how to make it work without calling customTryCatch on action?
You can either do this:
https://joulev.dev/blogs/throwing-expected-errors-in-react-server-actions
Or (what I like to use) use this:
https://next-safe-action.dev/
https://joulev.dev/blogs/throwing-expected-errors-in-react-server-actions
Or (what I like to use) use this:
https://next-safe-action.dev/
Answer
@American black bear thanks u so much
sure thing. Please mark solution