summaryrefslogtreecommitdiff
path: root/packages/backend/src
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2025-05-06 21:31:53 -0400
committerHazelnoot <acomputerdog@gmail.com>2025-05-08 11:23:20 -0400
commitfefe2f6db82f2e0aaa84c761c613e46a1b6f6b18 (patch)
treea586dd628237b205de70da7062e5c6ecd50ff128 /packages/backend/src
parentdifferentiate between "error" and "exception" in mastodon API (diff)
downloadsharkey-fefe2f6db82f2e0aaa84c761c613e46a1b6f6b18.tar.gz
sharkey-fefe2f6db82f2e0aaa84c761c613e46a1b6f6b18.tar.bz2
sharkey-fefe2f6db82f2e0aaa84c761c613e46a1b6f6b18.zip
more improvements to Mastodon error logging
Diffstat (limited to 'packages/backend/src')
-rw-r--r--packages/backend/src/server/api/mastodon/MastodonLogger.ts53
1 files changed, 36 insertions, 17 deletions
diff --git a/packages/backend/src/server/api/mastodon/MastodonLogger.ts b/packages/backend/src/server/api/mastodon/MastodonLogger.ts
index ef8788f9b7..5b4070eae9 100644
--- a/packages/backend/src/server/api/mastodon/MastodonLogger.ts
+++ b/packages/backend/src/server/api/mastodon/MastodonLogger.ts
@@ -4,11 +4,12 @@
*/
import { Injectable } from '@nestjs/common';
-import { FastifyRequest } from 'fastify';
-import Logger from '@/logger.js';
+import type Logger from '@/logger.js';
import { LoggerService } from '@/core/LoggerService.js';
import { ApiError } from '@/server/api/error.js';
import { getBaseUrl } from '@/server/api/mastodon/MastodonClientService.js';
+import { AuthenticationError } from '@/server/api/AuthenticateService.js';
+import type { FastifyRequest } from 'fastify';
@Injectable()
export class MastodonLogger {
@@ -53,24 +54,36 @@ export interface MastodonError {
}
export function getErrorException(error: unknown): Error | null {
- if (error instanceof Error && error.name === 'AxiosError') {
+ if (!(error instanceof Error)) {
+ return null;
+ }
+
+ // AxiosErrors need special decoding
+ if (error.name === 'AxiosError') {
// Axios errors with a response are from the remote
- if (!('response' in error) || !error.response || typeof (error.response) !== 'object') {
- // This is the inner exception, basically
- if ('cause' in error && error.cause instanceof Error) {
- return error.cause;
- }
+ if ('response' in error && error.response && typeof (error.response) === 'object') {
+ return null;
+ }
- const ex = new Error();
- ex.name = error.name;
- ex.stack = error.stack;
- ex.message = error.message;
- ex.cause = error.cause;
- return ex;
+ // This is the inner exception, basically
+ if ('cause' in error && error.cause instanceof Error) {
+ return error.cause;
}
+
+ const ex = new Error();
+ ex.name = error.name;
+ ex.stack = error.stack;
+ ex.message = error.message;
+ ex.cause = error.cause;
+ return ex;
}
- return null;
+ // AuthenticationError is a client error
+ if (error instanceof AuthenticationError) {
+ return null;
+ }
+
+ return error;
}
export function getErrorData(error: unknown): MastodonError {
@@ -107,7 +120,7 @@ export function getErrorData(error: unknown): MastodonError {
if ('error' in error && typeof(error.error) === 'string') {
// "error_description" is string, undefined, or not present.
if (!('error_description' in error) || typeof(error.error_description) === 'string' || typeof(error.error_description) === 'undefined') {
- return error as MastodonError;
+ return convertMastodonError(error as MastodonError);
}
}
@@ -161,12 +174,18 @@ function convertErrorMessageError(error: { error: string, message: string }): Ma
function convertGenericError(error: Error): MastodonError {
return {
- ...error,
error: 'INTERNAL_ERROR',
error_description: String(error),
};
}
+function convertMastodonError(error: MastodonError): MastodonError {
+ return {
+ error: error.error,
+ error_description: error.error_description,
+ };
+}
+
export function getErrorStatus(error: unknown): number {
if (error && typeof(error) === 'object') {
// Axios wraps errors from the backend