Merge pull request #15075 from maxbronnikov10/refactor/http-adapter-error

refactor(core,express,fastify): HTTP adapter error mapping
This commit is contained in:
Kamil Mysliwiec
2026-02-18 15:05:37 +01:00
committed by GitHub
7 changed files with 116 additions and 89 deletions

View File

@@ -165,6 +165,10 @@ export abstract class AbstractHttpAdapter<
public beforeClose(): void {}
public mapException(error: unknown): unknown {
return error;
}
abstract close();
abstract initHttpServer(options: NestApplicationOptions);
abstract useStaticAssets(...args: any[]);

View File

@@ -160,7 +160,7 @@ export class RoutesResolver implements Resolver {
res: TResponse,
next: Function,
) => {
throw this.mapExternalException(err);
throw this.container.getHttpAdapterRef().mapException(err);
};
const handler = this.routerExceptionsFilter.create(
{},
@@ -174,31 +174,6 @@ export class RoutesResolver implements Resolver {
applicationRef.setErrorHandler(proxy, prefix);
}
public mapExternalException(err: any) {
switch (true) {
// SyntaxError is thrown by Express body-parser when given invalid JSON (#422, #430)
// URIError is thrown by Express when given a path parameter with an invalid percentage
// 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(

View File

@@ -1,11 +1,4 @@
import { createError as createFastifyError } from '@fastify/error';
import {
BadRequestException,
HttpException,
Module,
Post,
VersioningType,
} from '@nestjs/common';
import { Module, Post, VersioningType } from '@nestjs/common';
import { MODULE_PATH } from '@nestjs/common/constants.js';
import { Controller } from '../../../common/decorators/core/controller.decorator.js';
import { Get } from '../../../common/decorators/http/request-mapping.decorator.js';
@@ -297,61 +290,6 @@ describe('RoutesResolver', () => {
});
});
describe('mapExternalExceptions', () => {
describe('when exception prototype is', () => {
describe('SyntaxError', () => {
it('should map to BadRequestException', () => {
const err = new SyntaxError();
const outputErr = routesResolver.mapExternalException(err);
expect(outputErr).toBeInstanceOf(BadRequestException);
});
});
describe('URIError', () => {
it('should map to BadRequestException', () => {
const err = new URIError();
const outputErr = routesResolver.mapExternalException(err);
expect(outputErr).toBeInstanceOf(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).toBeInstanceOf(HttpException);
expect(result.message).toBe(error.message);
expect(result.getStatus()).toBe(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).toBeInstanceOf(HttpException);
expect(result.message).toBe(error.message);
expect(result.getStatus()).toBe(500);
});
});
describe('other', () => {
it('should behave as an identity', () => {
const err = new Error();
const outputErr = routesResolver.mapExternalException(err);
expect(outputErr).toEqual(err);
});
});
});
});
describe('registerNotFoundHandler', () => {
it('should register not found handler', () => {
routesResolver.registerNotFoundHandler();

View File

@@ -1,4 +1,5 @@
import {
BadRequestException,
HttpStatus,
InternalServerErrorException,
Logger,
@@ -535,6 +536,18 @@ export class ExpressAdapter extends AbstractHttpAdapter<
throw new Error('Unsupported versioning options');
}
public mapException(error: unknown): unknown {
switch (true) {
// SyntaxError is thrown by Express body-parser when given invalid JSON (#422, #430)
// URIError is thrown by Express when given a path parameter with an invalid percentage
// encoding, e.g. '%FF' (#8915)
case error instanceof SyntaxError || error instanceof URIError:
return new BadRequestException(error.message);
default:
return error;
}
}
private trackOpenConnections() {
this.httpServer.on('connection', (socket: Duplex) => {
this.openConnections.add(socket);

View File

@@ -1,8 +1,14 @@
import { BadRequestException } from '@nestjs/common';
import { ExpressAdapter } from '@nestjs/platform-express';
import express from 'express';
describe('ExpressAdapter', () => {
afterEach(() => vi.restoreAllMocks());
let expressAdapter: ExpressAdapter;
beforeEach(() => {
expressAdapter = new ExpressAdapter();
});
describe('registerParserMiddleware', () => {
it('should register the express built-in parsers for json and urlencoded payloads', () => {
@@ -41,4 +47,24 @@ describe('ExpressAdapter', () => {
expect(useSpy).not.toHaveBeenCalled();
});
});
describe('mapException', () => {
it('should map URIError with status code to BadRequestException', () => {
const error = new URIError();
const result = expressAdapter.mapException(error) as BadRequestException;
expect(result).to.be.instanceOf(BadRequestException);
});
it('should map SyntaxError with status code to BadRequestException', () => {
const error = new SyntaxError();
const result = expressAdapter.mapException(error) as BadRequestException;
expect(result).to.be.instanceOf(BadRequestException);
});
it('should return error if it is not handler Error', () => {
const error = new Error('Test error');
const result = expressAdapter.mapException(error);
expect(result).to.equal(error);
});
});
});

View File

@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import { FastifyCorsOptions } from '@fastify/cors';
import {
BadRequestException,
HttpException,
HttpStatus,
Logger,
type RawBodyRequest,
@@ -13,6 +15,7 @@ import {
import {
FastifyBaseLogger,
FastifyBodyParser,
FastifyError,
FastifyInstance,
FastifyListenOptions,
FastifyPluginAsync,
@@ -748,6 +751,25 @@ export class FastifyAdapter<
return (this.instance.use as any)(...args);
}
public mapException(error: unknown): unknown {
if (this.isHttpFastifyError(error)) {
return new HttpException(error.message, error.statusCode);
}
return error;
}
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'
);
}
protected registerWithPrefix(
factory:
| FastifyPluginCallback<any>

View File

@@ -0,0 +1,49 @@
import { FastifyAdapter } from '../../adapters/fastify-adapter';
import { createError } from '@fastify/error';
import { HttpException } from '@nestjs/common';
describe('FastifyAdapter', () => {
let fastifyAdapter: FastifyAdapter;
beforeEach(() => {
fastifyAdapter = new FastifyAdapter();
});
afterEach(() => vi.restoreAllMocks());
describe('mapException', () => {
it('should map FastifyError with status code to HttpException', () => {
const FastifyErrorCls = createError(
'FST_ERR_CTP_INVALID_MEDIA_TYPE',
'Unsupported Media Type: %s',
415,
);
const error = new FastifyErrorCls();
const result = fastifyAdapter.mapException(error) as HttpException;
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 = createError(
'FST_WITHOUT_STATUS_CODE',
'Error without status code',
);
const error = new FastifyErrorCls();
const result = fastifyAdapter.mapException(error) as HttpException;
expect(result).to.be.instanceOf(HttpException);
expect(result.message).to.equal(error.message);
expect(result.getStatus()).to.equal(500);
});
it('should return error if it is not FastifyError', () => {
const error = new Error('Test error');
const result = fastifyAdapter.mapException(error);
expect(result).to.equal(error);
});
});
});