Skip to content

Commit d74324f

Browse files
committed
Normalize Fastify errors
This also suppresses error logs for log4shell attempts
1 parent f7957ca commit d74324f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/core/exception/exception.normalizer.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// eslint-disable-next-line no-restricted-imports,@seedcompany/no-restricted-imports
22
import * as Nest from '@nestjs/common';
3+
import * as Fastify from 'fastify';
34
import { InputException, ServerException } from '~/common';
45
import { ConstraintError } from '../database';
56
import { ExceptionNormalizer } from './exception.normalizer';
@@ -10,6 +11,23 @@ describe('ExceptionNormalizer', () => {
1011
const orig = sut.normalize.bind(sut);
1112
sut.normalize = (...args) => JSON.parse(JSON.stringify(orig(...args)));
1213

14+
describe('Fastify', () => {
15+
it('Client', () => {
16+
const ex = new Fastify.errorCodes.FST_ERR_CTP_BODY_TOO_LARGE();
17+
const res = sut.normalize({ ex });
18+
expect(res.message).toEqual('Request body is too large');
19+
expect(res.code).toEqual('FST_ERR_CTP_BODY_TOO_LARGE');
20+
expect(res.codes).toEqual(['FST_ERR_CTP_BODY_TOO_LARGE', 'Client']);
21+
});
22+
it('Server', () => {
23+
const ex = new Fastify.errorCodes.FST_ERR_HOOK_INVALID_TYPE();
24+
const res = sut.normalize({ ex });
25+
expect(res.message).toEqual('The hook name must be a string');
26+
expect(res.code).toEqual('FST_ERR_HOOK_INVALID_TYPE');
27+
expect(res.codes).toEqual(['FST_ERR_HOOK_INVALID_TYPE', 'Server']);
28+
});
29+
});
30+
1331
describe('HttpException', () => {
1432
it('simple', () => {
1533
const ex = new Nest.NotFoundException();

src/core/exception/exception.normalizer.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ export class ExceptionNormalizer {
174174
return { codes: ['GraphQL', 'Server'] };
175175
}
176176

177+
// Fastify convention
178+
if (
179+
'code' in ex &&
180+
typeof ex.code === 'string' &&
181+
ex.code.startsWith('FST_')
182+
) {
183+
const statusCode =
184+
'statusCode' in ex && typeof ex.statusCode === 'number'
185+
? ex.statusCode
186+
: undefined;
187+
const codes = [
188+
ex.code,
189+
statusCode && statusCode < 500 ? 'Client' : 'Server',
190+
];
191+
return { codes };
192+
}
193+
177194
// Fallback to generic Error
178195
return { codes: ['Server'] };
179196
}

0 commit comments

Comments
 (0)