UNPKG

@nestjs/microservices

Version:

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

69 lines (68 loc) • 3.29 kB
import { from as fromPromise, isObservable, of, throwError, } from 'rxjs'; import { catchError, mergeMap } from 'rxjs/operators'; import { BaseRpcExceptionFilter } from '../exceptions/base-rpc-exception-filter.js'; import { RpcException } from '../exceptions/rpc-exception.js'; import { ExecutionContextHost } from '@nestjs/core/internal'; const defaultExceptionFilter = new BaseRpcExceptionFilter(); /** * Handles what an exception filter threw, or rejected with, while it was * handling an error. No filter has handled that, so it is logged and * answered as an unknown error. An exception that returns itself from * `getError()` is a signal for the transport rather than an error, e.g. * `KafkaRetriableException` asking it to deliver the message again, so it * is passed on unchanged. */ export function handleExceptionFilterFailure(error) { if (error instanceof RpcException && error.getError() === error) { return throwError(() => error); } return defaultExceptionFilter.handleUnknownError(error, 'error'); } export class RpcProxy { create(targetCallback, exceptionsHandler, reportUnhandledErrors = false) { return async (...args) => { try { const result = await targetCallback(...args); return !isObservable(result) ? result : result.pipe(catchError(error => this.handleStreamError(exceptionsHandler, args, error, reportUnhandledErrors))); } catch (error) { // A filter that throws or rejects here rejects the handler promise, // which the transport answers as an unknown error and logs return this.handleError(exceptionsHandler, args, error, reportUnhandledErrors); } }; } handleError(exceptionsHandler, args, error, reportUnhandledErrors = false) { const host = new ExecutionContextHost(args); host.setType('rpc'); return exceptionsHandler.handle(error, host, reportUnhandledErrors); } /** * Runs the filters for the error of a response stream. What a filter * throws, or rejects with, would otherwise become the error of the stream, * which the transport cannot tell from an error reply the filter returned. */ handleStreamError(exceptionsHandler, args, error, reportUnhandledErrors) { let result; try { result = this.handleError(exceptionsHandler, args, error, reportUnhandledErrors); } catch (filterError) { return handleExceptionFilterFailure(filterError); } return this.transformToObservable(result); } // `catchError()` would emit the stream an async filter resolves to, iterate // a string or an array, and fail on other plain values. Resolve what the // filter returns as when the handler rejects instead. transformToObservable(result) { return isObservable(result) ? result : fromPromise(Promise.resolve(result)).pipe( // Ahead of "mergeMap": the error of the stream a filter resolves to // is its reply, not a failure of the filter catchError(handleExceptionFilterFailure), mergeMap(val => (isObservable(val) ? val : of(val)))); } }