|
1 | | -import { BadRequestException, NotFoundException } from '@nestjs/common'; |
| 1 | +import { |
| 2 | + BadRequestException, |
| 3 | + HttpException, |
| 4 | + NotFoundException, |
| 5 | +} from '@nestjs/common'; |
2 | 6 | import { |
3 | 7 | HOST_METADATA, |
4 | 8 | MODULE_PATH, |
@@ -27,6 +31,7 @@ import { RoutePathFactory } from './route-path-factory'; |
27 | 31 | import { RouterExceptionFilters } from './router-exception-filters'; |
28 | 32 | import { RouterExplorer } from './router-explorer'; |
29 | 33 | import { RouterProxy } from './router-proxy'; |
| 34 | +import { FastifyError } from 'fastify'; |
30 | 35 |
|
31 | 36 | export class RoutesResolver implements Resolver { |
32 | 37 | private readonly logger = new Logger(RoutesResolver.name, { |
@@ -185,11 +190,24 @@ export class RoutesResolver implements Resolver { |
185 | 190 | // encoding, e.g. '%FF' (#8915) |
186 | 191 | case err instanceof SyntaxError || err instanceof URIError: |
187 | 192 | return new BadRequestException(err.message); |
| 193 | + case this.isHttpFastifyError(err): |
| 194 | + return new HttpException(err.message, err.statusCode); |
188 | 195 | default: |
189 | 196 | return err; |
190 | 197 | } |
191 | 198 | } |
192 | 199 |
|
| 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 | + |
193 | 211 | private getModulePathMetadata(metatype: Type<unknown>): string | undefined { |
194 | 212 | const modulesContainer = this.container.getModules(); |
195 | 213 | const modulePath = Reflect.getMetadata( |
|
0 commit comments