1
1
import {
2
2
BadRequestException ,
3
+ HttpException ,
3
4
Module ,
4
5
Post ,
5
6
VersioningType ,
@@ -17,6 +18,7 @@ import { GraphInspector } from '../../inspector/graph-inspector';
17
18
import { SerializedGraph } from '../../inspector/serialized-graph' ;
18
19
import { RoutesResolver } from '../../router/routes-resolver' ;
19
20
import { NoopHttpAdapter } from '../utils/noop-adapter.spec' ;
21
+ import { createError } from '@fastify/error' ;
20
22
21
23
describe ( 'RoutesResolver' , ( ) => {
22
24
@Controller ( 'global' )
@@ -334,6 +336,35 @@ describe('RoutesResolver', () => {
334
336
expect ( outputErr ) . to . be . instanceof ( BadRequestException ) ;
335
337
} ) ;
336
338
} ) ;
339
+ describe ( 'FastifyError' , ( ) => {
340
+ it ( 'should map FastifyError with status code to HttpException' , ( ) => {
341
+ const FastifyErrorCls = createError (
342
+ 'FST_ERR_CTP_INVALID_MEDIA_TYPE' ,
343
+ 'Unsupported Media Type: %s' ,
344
+ 415 ,
345
+ ) ;
346
+ const error = new FastifyErrorCls ( ) ;
347
+
348
+ const result = routesResolver . mapExternalException ( error ) ;
349
+
350
+ expect ( result ) . to . be . instanceOf ( HttpException ) ;
351
+ expect ( result . message ) . to . equal ( error . message ) ;
352
+ expect ( result . getStatus ( ) ) . to . equal ( 415 ) ;
353
+ } ) ;
354
+
355
+ it ( 'should return FastifyError without user status code to Internal Server Error HttpException' , ( ) => {
356
+ const FastifyErrorCls = createError (
357
+ 'FST_WITHOUT_STATUS_CODE' ,
358
+ 'Error without status code' ,
359
+ ) ;
360
+ const error = new FastifyErrorCls ( ) ;
361
+
362
+ const result = routesResolver . mapExternalException ( error ) ;
363
+ expect ( result ) . to . be . instanceOf ( HttpException ) ;
364
+ expect ( result . message ) . to . equal ( error . message ) ;
365
+ expect ( result . getStatus ( ) ) . to . equal ( 500 ) ;
366
+ } ) ;
367
+ } ) ;
337
368
describe ( 'other' , ( ) => {
338
369
it ( 'should behave as an identity' , ( ) => {
339
370
const err = new Error ( ) ;
0 commit comments