1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.BaseExceptionFilterContext = void 0;
|
4 | const constants_1 = require("@nestjs/common/constants");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const iterare_1 = require("iterare");
|
7 | const context_creator_1 = require("../helpers/context-creator");
|
8 | const constants_2 = require("../injector/constants");
|
9 | class BaseExceptionFilterContext extends context_creator_1.ContextCreator {
|
10 | constructor(container) {
|
11 | super();
|
12 | this.container = container;
|
13 | }
|
14 | createConcreteContext(metadata, contextId = constants_2.STATIC_CONTEXT, inquirerId) {
|
15 | if ((0, shared_utils_1.isEmpty)(metadata)) {
|
16 | return [];
|
17 | }
|
18 | return (0, iterare_1.iterate)(metadata)
|
19 | .filter(instance => instance && ((0, shared_utils_1.isFunction)(instance.catch) || instance.name))
|
20 | .map(filter => this.getFilterInstance(filter, contextId, inquirerId))
|
21 | .filter(item => !!item)
|
22 | .map(instance => ({
|
23 | func: instance.catch.bind(instance),
|
24 | exceptionMetatypes: this.reflectCatchExceptions(instance),
|
25 | }))
|
26 | .toArray();
|
27 | }
|
28 | getFilterInstance(filter, contextId = constants_2.STATIC_CONTEXT, inquirerId) {
|
29 | const isObject = filter.catch;
|
30 | if (isObject) {
|
31 | return filter;
|
32 | }
|
33 | const instanceWrapper = this.getInstanceByMetatype(filter);
|
34 | if (!instanceWrapper) {
|
35 | return null;
|
36 | }
|
37 | const instanceHost = instanceWrapper.getInstanceByContextId(contextId, inquirerId);
|
38 | return instanceHost && instanceHost.instance;
|
39 | }
|
40 | getInstanceByMetatype(metatype) {
|
41 | if (!this.moduleContext) {
|
42 | return;
|
43 | }
|
44 | const collection = this.container.getModules();
|
45 | const moduleRef = collection.get(this.moduleContext);
|
46 | if (!moduleRef) {
|
47 | return;
|
48 | }
|
49 | return moduleRef.injectables.get(metatype);
|
50 | }
|
51 | reflectCatchExceptions(instance) {
|
52 | const prototype = Object.getPrototypeOf(instance);
|
53 | return (Reflect.getMetadata(constants_1.FILTER_CATCH_EXCEPTIONS, prototype.constructor) || []);
|
54 | }
|
55 | }
|
56 | exports.BaseExceptionFilterContext = BaseExceptionFilterContext;
|