bugfix(@nestjs/common) revert @Catch() validation

This commit is contained in:
Kamil Myśliwiec
2018-08-01 21:09:12 +02:00
parent d92fa0dee1
commit 9f77d5f550
4 changed files with 28 additions and 49 deletions

View File

@@ -1,20 +1,12 @@
import 'reflect-metadata';
import { FILTER_CATCH_EXCEPTIONS } from '../../constants';
import { Type } from '../../interfaces';
import { isNil } from '../../utils/shared.utils';
import { InvalidDecoratorItemException } from '../../utils/validate-each.util';
/**
* Defines the Exceptions Filter. Takes set of exception types as an argument which has to be caught by this Filter.
* The class should implement the `ExceptionFilter` interface.
*/
export function Catch(...exceptions: Type<any>[]): ClassDecorator {
const validateNil = item => {
if (isNil(item)) {
throw new InvalidDecoratorItemException('@Catch', `value (${item})`, 'runtime exception');
}
};
exceptions.forEach(validateNil);
return (target: object) => {
Reflect.defineMetadata(FILTER_CATCH_EXCEPTIONS, exceptions, target);
};

View File

@@ -2,7 +2,6 @@ import { expect } from 'chai';
import 'reflect-metadata';
import { FILTER_CATCH_EXCEPTIONS } from '../../constants';
import { Catch } from '../../decorators/core/catch.decorator';
import { InvalidDecoratorItemException } from '../../utils/validate-each.util';
describe('@Catch', () => {
const exceptions: any = ['exception', 'exception2'];
@@ -14,15 +13,4 @@ describe('@Catch', () => {
const metadata = Reflect.getMetadata(FILTER_CATCH_EXCEPTIONS, Test);
expect(metadata).to.be.eql(exceptions);
});
describe('when one item is nil', () => {
it('should throw an exception', () => {
try {
Catch(null)(Test);
}
catch (err) {
expect(err).to.be.instanceof(InvalidDecoratorItemException);
}
});
});
});

View File

@@ -11,6 +11,31 @@ import { MiddlewareWrapper } from '../middleware/container';
import { InstanceWrapper } from './container';
import { Module } from './module';
/**
* The type of an injectable dependency
*/
export type InjectorDependency = Type<any> | Function | string;
/**
* Context of a dependency which gets injected by
* the injector
*/
export interface InjectorDependencyContext {
/**
* The name of the function or injection token
*/
name?: string;
/**
* The index of the dependency which gets injected
* from the dependencies array
*/
index: number;
/**
* The dependency array which gets injected
*/
dependencies: InjectorDependency[];
}
export class Injector {
public async loadInstanceOfMiddleware(
wrapper: MiddlewareWrapper,
@@ -289,28 +314,3 @@ export class Injector {
return modules.concat.apply(modules, modules.map(flatten));
}
}
/**
* The type of an injectable dependency
*/
export type InjectorDependency = Type<any> | Function | string;
/**
* Context of a dependency which gets injected by
* the injector
*/
export interface InjectorDependencyContext {
/**
* The name of the function or injection token
*/
name?: string;
/**
* The index of the dependency which gets injected
* from the dependencies array
*/
index: number;
/**
* The dependency array which gets injected
*/
dependencies: InjectorDependency[];
}

View File

@@ -1,7 +1,6 @@
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';
import { HttpException } from '@nestjs/common';
import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/common';
@Catch(HttpException)
@Catch(3)
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();