mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
bugfix(@nestjs/common) revert @Catch() validation
This commit is contained in:
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
@@ -288,29 +313,4 @@ 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[];
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user