Skip to content

fix(core): HTTP adapter error mapping #15056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/core/router/routes-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BadRequestException, NotFoundException } from '@nestjs/common';
import {
BadRequestException,
HttpException,
NotFoundException,
} from '@nestjs/common';
import {
HOST_METADATA,
MODULE_PATH,
Expand Down Expand Up @@ -185,11 +189,24 @@ export class RoutesResolver implements Resolver {
// encoding, e.g. '%FF' (#8915)
case err instanceof SyntaxError || err instanceof URIError:
return new BadRequestException(err.message);
case this.isHttpFastifyError(err):
return new HttpException(err.message, err.statusCode);
default:
return err;
}
}

private isHttpFastifyError(
error: any,
): error is Error & { statusCode: number } {
// condition based on this code - https://github.com/fastify/fastify-error/blob/d669b150a82968322f9f7be992b2f6b463272de3/index.js#L22
return (
error.statusCode !== undefined &&
error instanceof Error &&
error.name === 'FastifyError'
);
}

private getModulePathMetadata(metatype: Type<unknown>): string | undefined {
const modulesContainer = this.container.getModules();
const modulePath = Reflect.getMetadata(
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/router/routes-resolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
BadRequestException,
HttpException,
Module,
Post,
VersioningType,
Expand All @@ -17,6 +18,7 @@ import { GraphInspector } from '../../inspector/graph-inspector';
import { SerializedGraph } from '../../inspector/serialized-graph';
import { RoutesResolver } from '../../router/routes-resolver';
import { NoopHttpAdapter } from '../utils/noop-adapter.spec';
import { createError as createFastifyError } from '@fastify/error';

describe('RoutesResolver', () => {
@Controller('global')
Expand Down Expand Up @@ -334,6 +336,35 @@ describe('RoutesResolver', () => {
expect(outputErr).to.be.instanceof(BadRequestException);
});
});
describe('FastifyError', () => {
it('should map FastifyError with status code to HttpException', () => {
const FastifyErrorCls = createFastifyError(
'FST_ERR_CTP_INVALID_MEDIA_TYPE',
'Unsupported Media Type: %s',
415,
);
const error = new FastifyErrorCls();

const result = routesResolver.mapExternalException(error);

expect(result).to.be.instanceOf(HttpException);
expect(result.message).to.equal(error.message);
expect(result.getStatus()).to.equal(415);
});

it('should return FastifyError without user status code to Internal Server Error HttpException', () => {
const FastifyErrorCls = createFastifyError(
'FST_WITHOUT_STATUS_CODE',
'Error without status code',
);
const error = new FastifyErrorCls();

const result = routesResolver.mapExternalException(error);
expect(result).to.be.instanceOf(HttpException);
expect(result.message).to.equal(error.message);
expect(result.getStatus()).to.equal(500);
});
});
describe('other', () => {
it('should behave as an identity', () => {
const err = new Error();
Expand Down