From b6f4fda80da4f08241b294656603de8f5a64179d Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Tue, 6 May 2025 20:55:54 -0400 Subject: [PATCH] handle AxiosErrors without a response --- .../src/server/api/mastodon/MastodonLogger.ts | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/MastodonLogger.ts b/packages/backend/src/server/api/mastodon/MastodonLogger.ts index 29d44918a3..54966f6236 100644 --- a/packages/backend/src/server/api/mastodon/MastodonLogger.ts +++ b/packages/backend/src/server/api/mastodon/MastodonLogger.ts @@ -57,10 +57,21 @@ export interface MastodonError { } export function getErrorException(error: unknown): Error | null { - if (error instanceof Error) { - // Axios errors are not exceptions - they're from the remote + if (error instanceof Error && error.name === 'AxiosError') { + // Axios errors with a response are from the remote if (!('response' in error) || !error.response || typeof (error.response) !== 'object') { - return error; + // This is the inner exception, basically + if ('cause' in error && error.cause instanceof Error) { + return error.cause; + } + + // Horrible hack to "recreate" the error without calling a constructor (since we want to re-use the stack). + return Object.assign(Object.create(Error), { + name: error.name, + stack: error.stack, + message: error.message, + cause: error.cause, + }); } } @@ -125,6 +136,22 @@ function unpackAxiosError(error: unknown): unknown { // No data - this is a fallback to avoid leaking request/response details in the error return undefined; } + + if (error instanceof Error && error.name === 'AxiosError') { + if ('cause' in error) { + return error.cause; + } + + if ('code' in error) { + return { + code: error.code, + message: String(error), + }; + } + + // No data - this is a fallback to avoid leaking request/response details in the error + return String(error); + } } return error;