UNPKG

1.51 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 select_exception_filter_metadata_util_1 = require("@nestjs/common/utils/select-exception-filter-metadata.util");
6const invalid_exception_filter_exception_1 = require("@nestjs/core/errors/exceptions/invalid-exception-filter.exception");
7const base_ws_exception_filter_1 = require("./base-ws-exception-filter");
8/**
9 * @publicApi
10 */
11class WsExceptionsHandler extends base_ws_exception_filter_1.BaseWsExceptionFilter {
12 constructor() {
13 super(...arguments);
14 this.filters = [];
15 }
16 handle(exception, host) {
17 const client = host.switchToWs().getClient();
18 if (this.invokeCustomFilters(exception, host) || !client.emit) {
19 return;
20 }
21 super.catch(exception, host);
22 }
23 setCustomFilters(filters) {
24 if (!Array.isArray(filters)) {
25 throw new invalid_exception_filter_exception_1.InvalidExceptionFilterException();
26 }
27 this.filters = filters;
28 }
29 invokeCustomFilters(exception, args) {
30 if ((0, shared_utils_1.isEmpty)(this.filters))
31 return false;
32 const filter = (0, select_exception_filter_metadata_util_1.selectExceptionFilterMetadata)(this.filters, exception);
33 filter && filter.func(exception, args);
34 return !!filter;
35 }
36}
37exports.WsExceptionsHandler = WsExceptionsHandler;