UNPKG

@nestjs/microservices

Version:

Microservices for NestJS: message and event handlers over Kafka, RabbitMQ, NATS, Redis, MQTT, gRPC or TCP.

75 lines (74 loc) • 2.76 kB
var GrpcExceptionFilter_1; import { __decorate } from "tslib"; import { Catch, IntrinsicException, Logger, } from '@nestjs/common'; import { isNumber, isObject } from '@nestjs/common/internal'; import { MESSAGES } from '@nestjs/core/internal'; import { throwError } from 'rxjs'; import { GrpcStatus } from '../enums/grpc-status.enum.js'; import { GrpcException } from './grpc-exception.js'; import { RpcException } from './rpc-exception.js'; /** * @publicApi */ let GrpcExceptionFilter = class GrpcExceptionFilter { static { GrpcExceptionFilter_1 = this; } static logger = new Logger('RpcExceptionsHandler'); catch(exception, host) { if (exception instanceof GrpcException) { return throwError(() => exception.getError()); } if (exception instanceof RpcException) { return throwError(() => this.serializeRpcException(exception)); } return throwError(() => ({ code: GrpcStatus.UNKNOWN, message: MESSAGES.UNKNOWN_EXCEPTION_MESSAGE, })); } serializeRpcException(exception) { const error = exception.getError(); // An Error, as in `new RpcException(err)`, was not written for the // caller: its message, and its "details" and "metadata" if it has them, // would be sent with the status. As in `BaseRpcExceptionFilter`, it is // logged and answered as an unknown error, except that its numeric "code" // or "status" is still used as the status. An exception that returns // itself from `getError()` holds no error other than its own. if (error instanceof Error && error !== exception) { if (!(error instanceof IntrinsicException)) { GrpcExceptionFilter_1.logger.error(error); } const code = this.getErrorCode(error); return { code: isNumber(code) ? code : GrpcStatus.UNKNOWN, message: MESSAGES.UNKNOWN_EXCEPTION_MESSAGE, }; } if (isObject(error)) { const code = this.getErrorCode(error); if (isNumber(code)) { return { ...error, code, message: exception.message, }; } } return { code: GrpcStatus.UNKNOWN, message: exception.message, }; } getErrorCode(error) { const { code, status } = error; if (isNumber(code)) { return code; } if (isNumber(status)) { return status; } } }; GrpcExceptionFilter = GrpcExceptionFilter_1 = __decorate([ Catch() ], GrpcExceptionFilter); export { GrpcExceptionFilter };