UNPKG

@nestjs/microservices

Version:

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

40 lines (39 loc) • 1.68 kB
import { IntrinsicException, Logger, } from '@nestjs/common'; import { throwError as _throw } from 'rxjs'; import { RpcException } from './rpc-exception.js'; import { isObject } from '@nestjs/common/internal'; import { MESSAGES } from '@nestjs/core/internal'; /** * @publicApi */ export class BaseRpcExceptionFilter { static logger = new Logger('RpcExceptionsHandler'); catch(exception, host) { const status = 'error'; if (!(exception instanceof RpcException)) { return this.handleUnknownError(exception, status); } const res = exception.getError(); // An Error, as in `new RpcException(err)`, would be sent with its // enumerable properties, e.g. the request config of an HTTP client error. // An exception that returns itself from `getError()` is a control signal // rather than a held error: `KafkaRetriableException` uses it to ask the // transport to redeliver the message, so it has to keep travelling. if (res instanceof Error && res !== exception) { return this.handleUnknownError(res, status); } const message = isObject(res) ? res : { status, message: res }; return _throw(() => message); } handleUnknownError(exception, status) { const errorMessage = MESSAGES.UNKNOWN_EXCEPTION_MESSAGE; if (!(exception instanceof IntrinsicException)) { const logger = BaseRpcExceptionFilter.logger; logger.error(exception); } return _throw(() => ({ status, message: errorMessage })); } isError(exception) { return !!(isObject(exception) && exception.message); } }