diff options
| author | Hazelnoot <acomputerdog@gmail.com> | 2025-05-06 20:55:54 -0400 |
|---|---|---|
| committer | Hazelnoot <acomputerdog@gmail.com> | 2025-05-08 11:23:20 -0400 |
| commit | b6f4fda80da4f08241b294656603de8f5a64179d (patch) | |
| tree | 8e336238d9639cd11b12c418a4f7f3c980b438b6 /packages/backend/src/server/api/mastodon | |
| parent | split exception logging from error logging to simplify and improve mastodon e... (diff) | |
| download | sharkey-b6f4fda80da4f08241b294656603de8f5a64179d.tar.gz sharkey-b6f4fda80da4f08241b294656603de8f5a64179d.tar.bz2 sharkey-b6f4fda80da4f08241b294656603de8f5a64179d.zip | |
handle AxiosErrors without a response
Diffstat (limited to 'packages/backend/src/server/api/mastodon')
| -rw-r--r-- | packages/backend/src/server/api/mastodon/MastodonLogger.ts | 33 |
1 files 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; |