diff --git a/src/core/exception/exception.normalizer.test.ts b/src/core/exception/exception.normalizer.test.ts index c288475561..f1b3ee95ad 100644 --- a/src/core/exception/exception.normalizer.test.ts +++ b/src/core/exception/exception.normalizer.test.ts @@ -1,5 +1,6 @@ // eslint-disable-next-line no-restricted-imports,@seedcompany/no-restricted-imports import * as Nest from '@nestjs/common'; +import * as Fastify from 'fastify'; import { InputException, ServerException } from '~/common'; import { ConstraintError } from '../database'; import { ExceptionNormalizer } from './exception.normalizer'; @@ -10,6 +11,23 @@ describe('ExceptionNormalizer', () => { const orig = sut.normalize.bind(sut); sut.normalize = (...args) => JSON.parse(JSON.stringify(orig(...args))); + describe('Fastify', () => { + it('Client', () => { + const ex = new Fastify.errorCodes.FST_ERR_CTP_BODY_TOO_LARGE(); + const res = sut.normalize({ ex }); + expect(res.message).toEqual('Request body is too large'); + expect(res.code).toEqual('FST_ERR_CTP_BODY_TOO_LARGE'); + expect(res.codes).toEqual(['FST_ERR_CTP_BODY_TOO_LARGE', 'Client']); + }); + it('Server', () => { + const ex = new Fastify.errorCodes.FST_ERR_HOOK_INVALID_TYPE(); + const res = sut.normalize({ ex }); + expect(res.message).toEqual('The hook name must be a string'); + expect(res.code).toEqual('FST_ERR_HOOK_INVALID_TYPE'); + expect(res.codes).toEqual(['FST_ERR_HOOK_INVALID_TYPE', 'Server']); + }); + }); + describe('HttpException', () => { it('simple', () => { const ex = new Nest.NotFoundException(); diff --git a/src/core/exception/exception.normalizer.ts b/src/core/exception/exception.normalizer.ts index 5af7e24cdd..8f2055c6e5 100644 --- a/src/core/exception/exception.normalizer.ts +++ b/src/core/exception/exception.normalizer.ts @@ -174,6 +174,15 @@ export class ExceptionNormalizer { return { codes: ['GraphQL', 'Server'] }; } + // Fastify convention + if ('statusCode' in ex && typeof ex.statusCode === 'number') { + const codes = [ + 'code' in ex && typeof ex.code === 'string' ? ex.code : undefined, + ex.statusCode < 500 ? 'Client' : 'Server', + ]; + return { codes }; + } + // Fallback to generic Error return { codes: ['Server'] }; }