1
- import { Controller , Get , Query , UsePipes , ValidationPipe } from '@nestjs/common' ;
1
+ import {
2
+ BadRequestException ,
3
+ Controller ,
4
+ Get ,
5
+ Query ,
6
+ UseFilters ,
7
+ UsePipes ,
8
+ ValidationPipe ,
9
+ } from '@nestjs/common' ;
2
10
import { ApiOperation , ApiTags } from '@nestjs/swagger' ;
3
- import { Cat } from './cats.models' ;
4
11
import { CatsProvider } from './cats.provider' ;
5
- import { CatReadResource } from './cats.resource' ;
6
12
import { Mapper } from 'packages/nest-utils/dist/main' ;
7
13
import { CatsFindManyQuery } from './cats.queries' ;
14
+ import { CatReadResource } from './resources/cat.read.resource' ;
15
+ import { Cat } from './models/cat.model' ;
16
+
17
+ import { Catch , ExceptionFilter , ArgumentsHost , HttpException , HttpStatus } from '@nestjs/common' ;
18
+ import { Response } from 'express' ;
19
+ import { ApiException } from '@nanogiants/nestjs-swagger-api-exception-decorator' ;
20
+
21
+ @Catch ( )
22
+ export class GlobalExceptionFilter implements ExceptionFilter {
23
+ catch ( exception : unknown , host : ArgumentsHost ) {
24
+ const ctx = host . switchToHttp ( ) ;
25
+ const response = ctx . getResponse < Response > ( ) ;
26
+
27
+ let status = HttpStatus . INTERNAL_SERVER_ERROR ;
28
+ let message = 'Internal server error' ;
29
+
30
+ if ( exception instanceof HttpException ) {
31
+ status = exception . getStatus ( ) ;
32
+ message = exception . message ;
33
+ }
34
+
35
+ response . status ( status ) . json ( {
36
+ statusCode : status ,
37
+ message : message ,
38
+ } ) ;
39
+ }
40
+ }
8
41
9
42
@Controller ( 'cats' )
10
43
@UsePipes (
@@ -16,7 +49,9 @@ import { CatsFindManyQuery } from './cats.queries';
16
49
whitelist : true ,
17
50
} ) ,
18
51
)
52
+ @UseFilters ( GlobalExceptionFilter )
19
53
@ApiTags ( 'Cats' )
54
+ @ApiException ( ( ) => BadRequestException )
20
55
export class CatsController {
21
56
constructor (
22
57
private readonly provider : CatsProvider ,
0 commit comments