UNPKG

2.05 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/* eslint-disable @typescript-eslint/no-use-before-define */
4const constants_1 = require("../../constants");
5const extend_metadata_util_1 = require("../../utils/extend-metadata.util");
6const shared_utils_1 = require("../../utils/shared.utils");
7const validate_each_util_1 = require("../../utils/validate-each.util");
8/**
9 * Decorator that binds exception filters to the scope of the controller or
10 * method, depending on its context.
11 *
12 * When `@UseFilters` is used at the controller level, the filter will be
13 * applied to every handler (method) in the controller.
14 *
15 * When `@UseFilters` is used at the individual handler level, the filter
16 * will apply only to that specific method.
17 *
18 * @param filters exception filter instance or class, or a list of exception
19 * filter instances or classes.
20 *
21 * @see [Exception filters](https://docs.nestjs.com/exception-filters)
22 *
23 * @usageNotes
24 * Exception filters can also be set up globally for all controllers and routes
25 * using `app.useGlobalFilters()`. [See here for details](https://docs.nestjs.com/exception-filters#binding-filters)
26 *
27 * @publicApi
28 */
29exports.UseFilters = (...filters) => addExceptionFiltersMetadata(...filters);
30function addExceptionFiltersMetadata(...filters) {
31 return (target, key, descriptor) => {
32 const isFilterValid = (filter) => filter &&
33 (shared_utils_1.isFunction(filter) || shared_utils_1.isFunction(filter.catch));
34 if (descriptor) {
35 validate_each_util_1.validateEach(target.constructor, filters, isFilterValid, '@UseFilters', 'filter');
36 extend_metadata_util_1.extendArrayMetadata(constants_1.EXCEPTION_FILTERS_METADATA, filters, descriptor.value);
37 return descriptor;
38 }
39 validate_each_util_1.validateEach(target, filters, isFilterValid, '@UseFilters', 'filter');
40 extend_metadata_util_1.extendArrayMetadata(constants_1.EXCEPTION_FILTERS_METADATA, filters, target);
41 return target;
42 };
43}