Skip to content

Commit f6fdf2d

Browse files
committed
feature(grpc): modify rpc exception filter so it forward Grpc exception
1 parent 6136e0b commit f6fdf2d

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

packages/microservices/exceptions/base-rpc-exception-filter.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,34 @@ import { isObject, isError } from '@nestjs/common/utils/shared.utils';
33
import { MESSAGES } from '@nestjs/core/constants';
44
import { Observable, throwError as _throw } from 'rxjs';
55
import { RpcException } from './rpc-exception';
6+
import { GrpcException } from './grpc-exceptions';
7+
import { GrpcStatus } from '../enums/grpc-status.enum';
68

79
export class BaseRpcExceptionFilter<T = any, R = any>
810
implements RpcExceptionFilter<T> {
911
private static readonly logger = new Logger('RpcExceptionsHandler');
1012

1113
catch(exception: T, host: ArgumentsHost): Observable<R> {
1214
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;
1527

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);
2133

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 });
2735
}
2836
}

0 commit comments

Comments
 (0)