# All files / common/decorators/core exception-filters.decorator.ts

100% Statements 17/17
100% Branches 5/5
100% Functions 4/4
100% Lines 17/17

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    451x

    1x
    1x
    1x

    1x
    6x
    6x
    7x

    6x
    1x

    1x

    1x

    5x
    5x
    5x

    1x
    6x
     import { EXCEPTION_FILTERS_METADATA } from '../../constants';
    import { ExceptionFilter } from '../../index';
    import { extendArrayMetadata } from '../../utils/extend-metadata.util';
    import { isFunction } from '../../utils/shared.utils';
    import { validateEach } from '../../utils/validate-each.util';

    const defineFiltersMetadata = (...filters: (Function | ExceptionFilter)[]) => {
      return (target: any, key?, descriptor?) => {
        const isFilterValid = filter =>
          filter && (isFunction(filter) || isFunction(filter.catch));

        if (descriptor) {
          validateEach(
            target.constructor,
            filters,
            isFilterValid,
            '@UseFilters',
            'filter',
          );
          extendArrayMetadata(
            EXCEPTION_FILTERS_METADATA,
            filters,
            descriptor.value,
          );
          return descriptor;
        }
        validateEach(target, filters, isFilterValid, '@UseFilters', 'filter');
        extendArrayMetadata(EXCEPTION_FILTERS_METADATA, filters, target);
        return target;
      };
    };

    /**
     * Setups exception filters to the chosen context.
     * When the `@UseFilters()` is used on the controller level:
     * - Exception Filter will be set up to every handler (every method)
     *
     * When the `@UseFilters()` is used on the handle level:
     * - Exception Filter will be set up only to specified method
     *
     * @param  {ExceptionFilter[]} ...filters
     */
    export const UseFilters = (...filters: (ExceptionFilter | Function)[]) =>
      defineFiltersMetadata(...filters);