Skip to content

Commit d1b523b

Browse files
author
maxbronnikov10
committed
fix(core): HTTP adapter error mapping
1 parent f2d8543 commit d1b523b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/core/router/routes-resolver.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { BadRequestException, NotFoundException } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
HttpException,
4+
NotFoundException,
5+
} from '@nestjs/common';
26
import {
37
HOST_METADATA,
48
MODULE_PATH,
@@ -27,6 +31,7 @@ import { RoutePathFactory } from './route-path-factory';
2731
import { RouterExceptionFilters } from './router-exception-filters';
2832
import { RouterExplorer } from './router-explorer';
2933
import { RouterProxy } from './router-proxy';
34+
import { FastifyError } from 'fastify';
3035

3136
export class RoutesResolver implements Resolver {
3237
private readonly logger = new Logger(RoutesResolver.name, {
@@ -185,11 +190,24 @@ export class RoutesResolver implements Resolver {
185190
// encoding, e.g. '%FF' (#8915)
186191
case err instanceof SyntaxError || err instanceof URIError:
187192
return new BadRequestException(err.message);
193+
case this.isHttpFastifyError(err):
194+
return new HttpException(err.message, err.statusCode);
188195
default:
189196
return err;
190197
}
191198
}
192199

200+
private isHttpFastifyError(
201+
error: unknown,
202+
): error is FastifyError & { statusCode: number } {
203+
// condition based on this code - https://github.com/fastify/fastify-error/blob/d669b150a82968322f9f7be992b2f6b463272de3/index.js#L22
204+
return (
205+
error instanceof Error &&
206+
error.name === 'FastifyError' &&
207+
(error as FastifyError).statusCode !== undefined
208+
);
209+
}
210+
193211
private getModulePathMetadata(metatype: Type<unknown>): string | undefined {
194212
const modulesContainer = this.container.getModules();
195213
const modulePath = Reflect.getMetadata(

0 commit comments

Comments
 (0)