From 7561499cb128de599a61a94c15163e948d538adb Mon Sep 17 00:00:00 2001 From: jpdjere Date: Mon, 6 May 2024 10:43:00 +0200 Subject: [PATCH] Clean up Zod error for display --- .../public/common/hooks/use_app_toasts.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts index 7a810fa9247d2..5a5601b3ffd09 100644 --- a/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts +++ b/x-pack/plugins/security_solution/public/common/hooks/use_app_toasts.ts @@ -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 @@ -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, ''); +}