more fixes to Mastodon logging

This commit is contained in:
Hazelnoot 2025-05-06 21:06:33 -04:00
parent b6f4fda80d
commit 9db39d449f
2 changed files with 8 additions and 19 deletions

View file

@ -51,6 +51,7 @@ export class MastodonApiServerService {
const data = getErrorData(error);
const status = getErrorStatus(error);
const exception = getErrorException(error);
if (exception) {
this.logger.exception(request, exception);
}
@ -60,7 +61,7 @@ export class MastodonApiServerService {
// Log error responses (including converted JSON exceptions)
fastify.addHook('onSend', (request, reply, payload, done) => {
if (reply.statusCode >= 400 && reply.statusCode <= 500) {
if (reply.statusCode >= 400) {
if (typeof(payload) === 'string' && String(reply.getHeader('content-type')).toLowerCase().includes('application/json')) {
const body = JSON.parse(payload);
const data = getErrorData(body);

View file

@ -14,10 +14,6 @@ import { getBaseUrl } from '@/server/api/mastodon/MastodonClientService.js';
export class MastodonLogger {
public readonly logger: Logger;
public get verbose() {
return this.logger.verbose;
}
constructor(
loggerService: LoggerService,
) {
@ -65,13 +61,12 @@ export function getErrorException(error: unknown): Error | null {
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,
});
const ex = new Error();
ex.name = error.name;
ex.stack = error.stack;
ex.message = error.message;
ex.cause = error.cause;
return ex;
}
}
@ -142,13 +137,6 @@ function unpackAxiosError(error: unknown): unknown {
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);
}