Next.js Discord

Discord Forum

Throwing an Error on server action make our production app error

Answered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
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?

3 Replies

Answer
@American black bear thanks u so much
sure thing. Please mark solution