@@ -3,26 +3,34 @@ import { isObject, isError } from '@nestjs/common/utils/shared.utils';
3
3
import { MESSAGES } from '@nestjs/core/constants' ;
4
4
import { Observable , throwError as _throw } from 'rxjs' ;
5
5
import { RpcException } from './rpc-exception' ;
6
+ import { GrpcException } from './grpc-exceptions' ;
7
+ import { GrpcStatus } from '../enums/grpc-status.enum' ;
6
8
7
9
export class BaseRpcExceptionFilter < T = any , R = any >
8
10
implements RpcExceptionFilter < T > {
9
11
private static readonly logger = new Logger ( 'RpcExceptionsHandler' ) ;
10
12
11
13
catch ( exception : T , host : ArgumentsHost ) : Observable < R > {
12
14
const status = 'error' ;
13
- if ( ! ( exception instanceof RpcException ) ) {
14
- const errorMessage = MESSAGES . UNKNOWN_EXCEPTION_MESSAGE ;
15
+ if ( exception instanceof RpcException ) {
16
+ const res = exception . getError ( ) ;
17
+ const message = isObject ( res ) ? res : { status, message : res } ;
18
+ return _throw ( message ) ;
19
+ }
20
+ if ( exception instanceof GrpcException ) {
21
+ const code = exception . getCode ( ) ;
22
+ const error = exception . getError ( ) ;
23
+ const message = isObject ( error ) ? error : { code, message : error } ;
24
+ return _throw ( message ) ;
25
+ }
26
+ const errorMessage = MESSAGES . UNKNOWN_EXCEPTION_MESSAGE ;
15
27
16
- const loggerArgs = isError ( exception )
17
- ? [ exception . message , exception . stack ]
18
- : [ exception ] ;
19
- const logger = BaseRpcExceptionFilter . logger ;
20
- logger . error . apply ( logger , loggerArgs as any ) ;
28
+ const loggerArgs = isError ( exception )
29
+ ? [ exception . message , exception . stack ]
30
+ : [ exception ] ;
31
+ const logger = BaseRpcExceptionFilter . logger ;
32
+ logger . error . apply ( logger , loggerArgs as any ) ;
21
33
22
- return _throw ( { status, message : errorMessage } ) ;
23
- }
24
- const res = exception . getError ( ) ;
25
- const message = isObject ( res ) ? res : { status, message : res } ;
26
- return _throw ( message ) ;
34
+ return _throw ( { status, code : GrpcStatus . UNKNOWN , message : errorMessage } ) ;
27
35
}
28
36
}
0 commit comments