@@ -5,9 +5,11 @@ import {
55 ArgumentsHost ,
66 Catch ,
77 ExceptionFilter ,
8+ ForbiddenException ,
89 HttpException ,
910 HttpStatus ,
1011 Logger ,
12+ UnauthorizedException ,
1113} from '@nestjs/common' ;
1214
1315import { HttpExceptionWithErrorCodeType } from './http-exeception-with-error-code.type' ;
@@ -16,25 +18,31 @@ import { HttpExceptionWithErrorCodeType } from './http-exeception-with-error-cod
1618export class CatchAllExceptionFilter implements ExceptionFilter {
1719 private readonly logger = new Logger ( CatchAllExceptionFilter . name ) ;
1820
19- catch ( exception : HttpExceptionWithErrorCodeType | ZodValidationException , host : ArgumentsHost ) {
21+ catch ( exception : unknown , host : ArgumentsHost ) {
2022 const ctx = host . switchToHttp ( ) ;
2123 const response = ctx . getResponse < Response > ( ) ;
2224 const request = ctx . getRequest < Request > ( ) ;
2325
2426 let status = HttpStatus . INTERNAL_SERVER_ERROR ;
25- if ( exception instanceof HttpException && exception . getStatus ) {
27+ if ( exception instanceof HttpException ) {
2628 status = exception . getStatus ( ) ;
2729 }
2830
29- let errorMessage : string | string [ ] ;
31+ let errorMessage : string | string [ ] = 'Internal server error' ;
3032 let errorCode : string = 'E000' ;
33+
3134 if ( status === HttpStatus . FORBIDDEN ) {
3235 errorMessage = 'Forbidden' ;
33- } else {
36+ } else if ( exception instanceof Error ) {
3437 errorMessage = exception . message ;
35- if ( exception instanceof HttpExceptionWithErrorCodeType ) {
36- errorCode = exception . errorCode ;
37- }
38+ }
39+
40+ if ( exception instanceof HttpExceptionWithErrorCodeType ) {
41+ errorCode = exception . errorCode ;
42+ } else if ( exception instanceof UnauthorizedException ) {
43+ errorCode = 'E401' ;
44+ } else if ( exception instanceof ForbiddenException ) {
45+ errorCode = 'E403' ;
3846 }
3947
4048 if ( exception instanceof ZodValidationException ) {
@@ -46,22 +54,29 @@ export class CatchAllExceptionFilter implements ExceptionFilter {
4654 } ) ;
4755
4856 response . status ( status ) . json ( exception . getResponse ( ) ) ;
49- } else if ( exception instanceof HttpExceptionWithErrorCodeType ) {
50- response . status ( status ) . json ( {
51- timestamp : new Date ( ) . toISOString ( ) ,
52- path : request . url ,
53- message : errorMessage ,
54- errorCode,
55- } ) ;
56- } else {
57- this . logger . error ( exception ) ;
57+ return ;
58+ }
5859
60+ if (
61+ exception instanceof HttpExceptionWithErrorCodeType ||
62+ exception instanceof HttpException
63+ ) {
5964 response . status ( status ) . json ( {
6065 timestamp : new Date ( ) . toISOString ( ) ,
6166 path : request . url ,
6267 message : errorMessage ,
6368 errorCode,
6469 } ) ;
70+ return ;
6571 }
72+
73+ this . logger . error ( exception ) ;
74+
75+ response . status ( status ) . json ( {
76+ timestamp : new Date ( ) . toISOString ( ) ,
77+ path : request . url ,
78+ message : 'Internal server error' ,
79+ errorCode : 'E500' ,
80+ } ) ;
6681 }
6782}
0 commit comments