UNPKG

1.55 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.WsExceptionsHandler = void 0;
4const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
5const invalid_exception_filter_exception_1 = require("@nestjs/core/errors/exceptions/invalid-exception-filter.exception");
6const base_ws_exception_filter_1 = require("./base-ws-exception-filter");
7/**
8 * @publicApi
9 */
10class WsExceptionsHandler extends base_ws_exception_filter_1.BaseWsExceptionFilter {
11 constructor() {
12 super(...arguments);
13 this.filters = [];
14 }
15 handle(exception, host) {
16 const client = host.switchToWs().getClient();
17 if (this.invokeCustomFilters(exception, host) || !client.emit) {
18 return;
19 }
20 super.catch(exception, host);
21 }
22 setCustomFilters(filters) {
23 if (!Array.isArray(filters)) {
24 throw new invalid_exception_filter_exception_1.InvalidExceptionFilterException();
25 }
26 this.filters = filters;
27 }
28 invokeCustomFilters(exception, args) {
29 if ((0, shared_utils_1.isEmpty)(this.filters))
30 return false;
31 const filter = this.filters.find(({ exceptionMetatypes }) => {
32 const hasMetatype = !exceptionMetatypes.length ||
33 exceptionMetatypes.some(ExceptionMetatype => exception instanceof ExceptionMetatype);
34 return hasMetatype;
35 });
36 filter && filter.func(exception, args);
37 return !!filter;
38 }
39}
40exports.WsExceptionsHandler = WsExceptionsHandler;