1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ExternalExceptionsHandler = 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 external_exception_filter_1 = require("./external-exception-filter");
|
7 | class ExternalExceptionsHandler extends external_exception_filter_1.ExternalExceptionFilter {
|
8 | constructor() {
|
9 | super(...arguments);
|
10 | this.filters = [];
|
11 | }
|
12 | next(exception, host) {
|
13 | const result = this.invokeCustomFilters(exception, host);
|
14 | if (result) {
|
15 | return result;
|
16 | }
|
17 | return super.catch(exception, host);
|
18 | }
|
19 | setCustomFilters(filters) {
|
20 | if (!Array.isArray(filters)) {
|
21 | throw new invalid_exception_filter_exception_1.InvalidExceptionFilterException();
|
22 | }
|
23 | this.filters = filters;
|
24 | }
|
25 | invokeCustomFilters(exception, host) {
|
26 | if ((0, shared_utils_1.isEmpty)(this.filters)) {
|
27 | return null;
|
28 | }
|
29 | const isInstanceOf = (metatype) => exception instanceof metatype;
|
30 | const filter = this.filters.find(({ exceptionMetatypes }) => {
|
31 | const typeExists = !exceptionMetatypes.length || exceptionMetatypes.some(isInstanceOf);
|
32 | return typeExists;
|
33 | });
|
34 | return filter ? filter.func(exception, host) : null;
|
35 | }
|
36 | }
|
37 | exports.ExternalExceptionsHandler = ExternalExceptionsHandler;
|