feat: add logging

This commit is contained in:
2026-08-14 17:12:15 +03:00
parent 34c2b79e27
commit 25d7c5780b
25 changed files with 753 additions and 108 deletions
@@ -6,10 +6,11 @@ describe('HttpExceptionFilter', () => {
const status = jest.fn().mockReturnThis();
const type = jest.fn().mockReturnThis();
const json = jest.fn();
const locals: Record<string, unknown> = {};
const filter = new HttpExceptionFilter();
const host = {
switchToHttp: () => ({
getResponse: () => ({ status, type, json }),
getResponse: () => ({ status, type, json, locals }),
}),
} as unknown as ArgumentsHost;
@@ -21,5 +22,8 @@ describe('HttpExceptionFilter', () => {
status: 500,
code: 'INTERNAL_SERVER_ERROR',
});
expect(locals.publicErrorCode).toBe('INTERNAL_SERVER_ERROR');
expect(typeof locals.errorStack).toBe('string');
expect(locals.errorStack).not.toContain('database password: secret');
});
});
+13 -1
View File
@@ -23,12 +23,24 @@ export class HttpExceptionFilter implements ExceptionFilter {
const isValidation = Array.isArray(
(payload as { message?: unknown })?.message,
);
const code = isValidation
? 'VALIDATION_ERROR'
: this.codeFor(status, payload);
(response.locals ??= {}).publicErrorCode = code;
if (
status >= Number(HttpStatus.INTERNAL_SERVER_ERROR) &&
exception instanceof Error
)
response.locals.errorStack = exception.stack
?.split('\n')
.slice(1)
.join('\n');
response
.status(status)
.type('application/problem+json')
.json({
status,
code: isValidation ? 'VALIDATION_ERROR' : this.codeFor(status, payload),
code,
...(isValidation
? {
errors: this.validationErrors(