From 46a61dc51a8245aba2314b4e7d269475c5db7756 Mon Sep 17 00:00:00 2001 From: doomsower <12031673+doomsower@users.noreply.github.com> Date: Wed, 25 Dec 2024 09:34:30 -0300 Subject: [PATCH] fix: unwrap errors and causes --- src/errors/ErrorHandler.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/errors/ErrorHandler.ts b/src/errors/ErrorHandler.ts index b02510f..b1bb391 100644 --- a/src/errors/ErrorHandler.ts +++ b/src/errors/ErrorHandler.ts @@ -63,7 +63,10 @@ export class ErrorHandler { longMessage: error.message, }; } - const longMessage = error instanceof Error ? error.message : `${error}`; + if (error instanceof Error) { + return this.#unwrapCause(error); + } + const longMessage = `${error}`; const shortMessage = longMessage.split("\n")[0].slice(0, 128); return { longMessage, @@ -159,6 +162,16 @@ export class ErrorHandler { } } + #unwrapCause(e: Error): Pick { + const shortMessage = e.message.split("\n")[0].slice(0, 128); + let longMessage = e.message; + if (e.cause) { + const cause = this.#unwrapCause(e.cause as Error); + longMessage = longMessage + "Cause: " + cause.longMessage; + } + return { shortMessage, longMessage }; + } + #caLogger(ca?: CreditAccountData): ILogger { return ca ? this.log.child({