# All files / core/exceptions base-exception-filter-context.ts

100% Statements 27/27
94.12% Branches 16/17
100% Functions 8/8
100% Lines 27/27

    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    641x

    1x
    1x
    1x

    1x

    20x
    20x

    19x
    17x

    2x

    4x

    2x
    2x

    5x
    5x
    3x

    2x
    2x

    3x
    1x

    2x
    2x
    2x
    1x

    1x

    3x
    3x

     import { FILTER_CATCH_EXCEPTIONS } from '@nestjs/common/constants';
    import { Type } from '@nestjs/common/interfaces';
    import { ExceptionFilter } from '@nestjs/common/interfaces/exceptions/exception-filter.interface';
    import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
    import iterate from 'iterare';
    import { ContextCreator } from '../helpers/context-creator';
    import { NestContainer } from '../injector/container';

    export class BaseExceptionFilterContext extends ContextCreator {
      protected moduleContext: string;

      constructor(private readonly container: NestContainer) {
        super();
      }

      public createConcreteContext<T extends any[], R extends any[]>(
        metadata: T,
      ): R {
        if (isEmpty(metadata)) {
          return [] as R;
        }
        return iterate(metadata)
          .filter(
            instance => instance && (isFunction(instance.catch) || instance.name),
          )
          .map(filter => this.getFilterInstance(filter))
          .map(instance => ({
            func: instance.catch.bind(instance),
            exceptionMetatypes: this.reflectCatchExceptions(instance),
          }))
          .toArray() as R;
      }

      public getFilterInstance(filter: Function | ExceptionFilter) {
        const isObject = (filter as ExceptionFilter).catch;
        if (isObject) {
          return filter;
        }
        const instanceWrapper = this.getInstanceByMetatype(filter);
        return instanceWrapper && instanceWrapper.instance
          ? instanceWrapper.instance
          : null;
      }

      public getInstanceByMetatype(filter): { instance: any } | undefined {
        if (!this.moduleContext) {
          return undefined;
        }
        const collection = this.container.getModules();
        const module = collection.get(this.moduleContext);
        if (!module) {
          return undefined;
        }
        return module.injectables.get(filter.name);
      }

      public reflectCatchExceptions(instance: ExceptionFilter): Type<any>[] {
        const prototype = Object.getPrototypeOf(instance);
        return (
          Reflect.getMetadata(FILTER_CATCH_EXCEPTIONS, prototype.constructor) || []
        );
      }
    }