1- import { HttpException , HttpServer , Logger } from '@nestjs/common' ;
1+ import { HttpException , HttpServer } from '@nestjs/common' ;
22import { ExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions/exception-filter-metadata.interface' ;
33import { ArgumentsHost } from '@nestjs/common/interfaces/features/arguments-host.interface' ;
4- import { isEmpty , isObject } from '@nestjs/common/utils/shared.utils' ;
5- import { messages } from '../constants' ;
4+ import { isEmpty } from '@nestjs/common/utils/shared.utils' ;
65import { InvalidExceptionFilterException } from '../errors/exceptions/invalid-exception-filter.exception' ;
6+ import { BaseExceptionFilter } from './base-exception-filter' ;
77
8- export class ExceptionsHandler {
9- private static readonly logger = new Logger ( ExceptionsHandler . name ) ;
8+ export class ExceptionsHandler extends BaseExceptionFilter {
109 private filters : ExceptionFilterMetadata [ ] = [ ] ;
1110
12- constructor ( private readonly applicationRef : HttpServer ) { }
11+ constructor ( applicationRef : HttpServer ) {
12+ super ( applicationRef ) ;
13+ }
1314
1415 public next ( exception : Error | HttpException | any , ctx : ArgumentsHost ) {
15- if ( this . invokeCustomFilters ( exception , ctx ) ) return ;
16-
17- if ( ! ( exception instanceof HttpException ) ) {
18- const body = {
19- statusCode : 500 ,
20- message : messages . UNKNOWN_EXCEPTION_MESSAGE ,
21- } ;
22- this . applicationRef . reply ( ctx . getArgByIndex ( 1 ) , body , body . statusCode ) ;
23- if ( this . isExceptionObject ( exception ) ) {
24- return ExceptionsHandler . logger . error (
25- exception . message ,
26- exception . stack ,
27- ) ;
28- }
29- return ExceptionsHandler . logger . error ( exception ) ;
16+ if ( this . invokeCustomFilters ( exception , ctx ) ) {
17+ return void 0 ;
3018 }
31- const res = exception . getResponse ( ) ;
32- const message = isObject ( res )
33- ? res
34- : {
35- statusCode : exception . getStatus ( ) ,
36- message : res ,
37- } ;
38-
39- this . applicationRef . reply (
40- ctx . getArgByIndex ( 1 ) ,
41- message ,
42- exception . getStatus ( ) ,
43- ) ;
19+ super . catch ( exception , ctx ) ;
4420 }
4521
4622 public setCustomFilters ( filters : ExceptionFilterMetadata [ ] ) {
@@ -64,8 +40,4 @@ export class ExceptionsHandler {
6440 filter && filter . func ( exception , response ) ;
6541 return ! ! filter ;
6642 }
67-
68- public isExceptionObject ( err ) : err is Error {
69- return isObject ( err ) && ! ! ( err as Error ) . message ;
70- }
7143}
0 commit comments