UNPKG

1.54 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.BaseRpcExceptionFilter = void 0;
4/* eslint-disable prefer-spread */
5const common_1 = require("@nestjs/common");
6const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
7const constants_1 = require("@nestjs/core/constants");
8const rxjs_1 = require("rxjs");
9const rpc_exception_1 = require("./rpc-exception");
10/**
11 * @publicApi
12 */
13class BaseRpcExceptionFilter {
14 catch(exception, host) {
15 const status = 'error';
16 if (!(exception instanceof rpc_exception_1.RpcException)) {
17 return this.handleUnknownError(exception, status);
18 }
19 const res = exception.getError();
20 const message = (0, shared_utils_1.isObject)(res) ? res : { status, message: res };
21 return (0, rxjs_1.throwError)(() => message);
22 }
23 handleUnknownError(exception, status) {
24 const errorMessage = constants_1.MESSAGES.UNKNOWN_EXCEPTION_MESSAGE;
25 const loggerArgs = this.isError(exception)
26 ? [exception.message, exception.stack]
27 : [exception];
28 const logger = BaseRpcExceptionFilter.logger;
29 logger.error.apply(logger, loggerArgs);
30 return (0, rxjs_1.throwError)(() => ({ status, message: errorMessage }));
31 }
32 isError(exception) {
33 return !!((0, shared_utils_1.isObject)(exception) && exception.message);
34 }
35}
36exports.BaseRpcExceptionFilter = BaseRpcExceptionFilter;
37BaseRpcExceptionFilter.logger = new common_1.Logger('RpcExceptionsHandler');