1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ExceptionsHandler = void 0;
|
4 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
5 | const invalid_exception_filter_exception_1 = require("../errors/exceptions/invalid-exception-filter.exception");
|
6 | const base_exception_filter_1 = require("./base-exception-filter");
|
7 | class ExceptionsHandler extends base_exception_filter_1.BaseExceptionFilter {
|
8 | constructor() {
|
9 | super(...arguments);
|
10 | this.filters = [];
|
11 | }
|
12 | next(exception, ctx) {
|
13 | if (this.invokeCustomFilters(exception, ctx)) {
|
14 | return;
|
15 | }
|
16 | super.catch(exception, ctx);
|
17 | }
|
18 | setCustomFilters(filters) {
|
19 | if (!Array.isArray(filters)) {
|
20 | throw new invalid_exception_filter_exception_1.InvalidExceptionFilterException();
|
21 | }
|
22 | this.filters = filters;
|
23 | }
|
24 | invokeCustomFilters(exception, ctx) {
|
25 | if ((0, shared_utils_1.isEmpty)(this.filters)) {
|
26 | return false;
|
27 | }
|
28 | const isInstanceOf = (metatype) => exception instanceof metatype;
|
29 | const filter = this.filters.find(({ exceptionMetatypes }) => {
|
30 | const typeExists = !exceptionMetatypes.length || exceptionMetatypes.some(isInstanceOf);
|
31 | return typeExists;
|
32 | });
|
33 | filter && filter.func(exception, ctx);
|
34 | return !!filter;
|
35 | }
|
36 | }
|
37 | exports.ExceptionsHandler = ExceptionsHandler;
|