debug-log mastodon error responses

This commit is contained in:
Hazelnoot 2025-05-06 17:27:39 -04:00
parent d9d0adbc6f
commit 2aa3cf2731
2 changed files with 19 additions and 8 deletions

View file

@ -46,17 +46,21 @@ export class MastodonApiServerService {
this.serverUtilityService.addCORS(fastify); this.serverUtilityService.addCORS(fastify);
this.serverUtilityService.addFlattenedQueryType(fastify); this.serverUtilityService.addFlattenedQueryType(fastify);
fastify.setErrorHandler((error, request, reply) => { // Convert JS exceptions into error responses
try { fastify.setErrorHandler((error, _, reply) => {
const data = getErrorData(error); const data = getErrorData(error);
const status = getErrorStatus(error); const status = getErrorStatus(error);
this.logger.error(request, data, status);
reply.code(status).send(data); reply.code(status).send(data);
} catch (e) { });
this.logger.logger.error('Recursive error in mastodon API - this is a bug!', { e }, true);
// Log error responses (including converted JSON exceptions)
fastify.addHook('onSend', (request, reply, payload, done) => {
if (reply.statusCode >= 400) {
const data = getErrorData(payload);
this.logger.error(request, data, reply.statusCode);
} }
done();
}); });
// External endpoints // External endpoints

View file

@ -65,6 +65,13 @@ export function getErrorData(error: unknown): MastodonError {
return convertGenericError(error); return convertGenericError(error);
} }
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 convertUnknownError(error); return convertUnknownError(error);
} }