UNPKG

@nestjs/microservices

Version:

Nest - modern, fast, powerful node.js web framework (@microservices)

32 lines (31 loc) 1.16 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(); 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); } }