Skip to content

Commit

Permalink
Clean up Zod error for display
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdjere committed May 6, 2024
1 parent 209318e commit 7561499
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ export const appErrorToErrorStack = (error: AppError): Error => {
: '';
const stringifiedError = getStringifiedStack(error);
const adaptedError = new Error(
`${String(error.body.message).trim() !== '' ? error.body.message : error.message} ${statusCode}`
postprocessErrorString(
`${
String(error.body.message).trim() !== '' ? error.body.message : error.message
} ${statusCode}`
)
);
// Note although all the Typescript typings say that error.name is a string and exists, we still can encounter an undefined so we
// do an extra guard here and default to empty string if it is undefined
Expand Down Expand Up @@ -239,3 +243,8 @@ export const isEmptyObjectWhenStringified = (item: unknown): boolean => {
return false;
}
};

function postprocessErrorString(str: string): string {
// Remove the `[request body]` prefix added by Zod for request validation errors
return str.replace(/\[request body\]:/g, '');
}

0 comments on commit 7561499

Please sign in to comment.