From cb931988e4ecef4e35013b5cc1edb80dfca63b64 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 27 Mar 2025 10:58:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Improve=20error=20details=20extr?= =?UTF-8?q?action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/lib/src/parseUnknownError.ts | 46 +++++++++++++++++---------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/lib/src/parseUnknownError.ts b/packages/lib/src/parseUnknownError.ts index 01bea45be..39232056a 100644 --- a/packages/lib/src/parseUnknownError.ts +++ b/packages/lib/src/parseUnknownError.ts @@ -21,24 +21,10 @@ export const parseUnknownError = async ({ details: undefined, }; if (err instanceof Error) { - if ( - "response" in err && - typeof err.response === "object" && - err.response && - "text" in err.response && - typeof err.response.text === "function" - ) { - return { - context, - description: err.message, - details: await (err.response as Response).text(), - }; - } return { context, description: err.message, - details: - typeof err.cause === "string" ? err.cause : JSON.stringify(err.cause), + details: await extractDetails(err), }; } return { @@ -73,8 +59,7 @@ export const parseUnknownErrorSync = ({ return { context, description: err.message, - details: - typeof err.cause === "string" ? err.cause : JSON.stringify(err.cause), + details: extractDetailsSync(err), }; } return { @@ -89,3 +74,30 @@ export const parseUnknownErrorSync = ({ }; } }; + +const extractDetailsSync = (err: Error): string => { + if ("responseBody" in err) { + return typeof err.responseBody === "string" + ? err.responseBody + : JSON.stringify(err.responseBody); + } + return typeof err.cause === "string" ? err.cause : JSON.stringify(err.cause); +}; + +const extractDetails = async (err: Error): Promise => { + if ("responseBody" in err) { + return typeof err.responseBody === "string" + ? err.responseBody + : JSON.stringify(err.responseBody); + } + if ( + "response" in err && + typeof err.response === "object" && + err.response && + "text" in err.response && + typeof err.response.text === "function" + ) { + return await (err.response as Response).text(); + } + return typeof err.cause === "string" ? err.cause : JSON.stringify(err.cause); +};