correctly parse response errors for logging
This commit is contained in:
parent
2aa3cf2731
commit
c0f24eaf5d
3 changed files with 12 additions and 9 deletions
|
|
@ -56,9 +56,12 @@ export class MastodonApiServerService {
|
|||
|
||||
// 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);
|
||||
if (reply.statusCode >= 500 || (reply.statusCode >= 400 && this.logger.verbose)) {
|
||||
if (typeof(payload) === 'string' && String(reply.getHeader('content-type')).toLowerCase().includes('application/json')) {
|
||||
const body = JSON.parse(payload);
|
||||
const data = getErrorData(body);
|
||||
this.logger.error(request, data, reply.statusCode);
|
||||
}
|
||||
}
|
||||
done();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -3,22 +3,22 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { FastifyRequest } from 'fastify';
|
||||
import Logger from '@/logger.js';
|
||||
import { LoggerService } from '@/core/LoggerService.js';
|
||||
import { ApiError } from '@/server/api/error.js';
|
||||
import { EnvService } from '@/core/EnvService.js';
|
||||
import { getBaseUrl } from '@/server/api/mastodon/MastodonClientService.js';
|
||||
|
||||
@Injectable()
|
||||
export class MastodonLogger {
|
||||
public readonly logger: Logger;
|
||||
|
||||
constructor(
|
||||
@Inject(EnvService)
|
||||
private readonly envService: EnvService,
|
||||
public get verbose() {
|
||||
return this.logger.verbose;
|
||||
}
|
||||
|
||||
constructor(
|
||||
loggerService: LoggerService,
|
||||
) {
|
||||
this.logger = loggerService.getLogger('masto-api');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue