Skip to content

Commit

Permalink
bugfix(@nestjs/core) exceptions passed in done() function, SyntaxError
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 18, 2018
1 parent 8235a03 commit cfa4bff
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/core/router/routes-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { MetadataScanner } from '../metadata-scanner';
import { RouterExplorer } from './interfaces/explorer.inteface';
import { ExpressRouterExplorer } from './router-explorer';
import { ApplicationConfig } from './../application-config';
import { NotFoundException } from '@nestjs/common';
import { NotFoundException, BadRequestException } from '@nestjs/common';
import { MODULE_PATH } from '@nestjs/common/constants';

export class RoutesResolver implements Resolver {
Expand Down Expand Up @@ -43,8 +43,8 @@ export class RoutesResolver implements Resolver {
: undefined;
this.setupRouters(routes, moduleName, path, router);
});

this.setupNotFoundHandler(router);
this.setupExceptionHandler(router);
this.setupExceptionHandler(express);
}

Expand Down Expand Up @@ -79,7 +79,7 @@ export class RoutesResolver implements Resolver {

public setupExceptionHandler(express: Application) {
const callback = (err, req, res, next) => {
throw err;
throw this.mapExternalException(err);
};
const exceptionHandler = this.routerExceptionsFilter.create(
{},
Expand All @@ -91,4 +91,13 @@ export class RoutesResolver implements Resolver {
);
express.use(proxy);
}

public mapExternalException(err: any) {
switch (true) {
case (err instanceof SyntaxError):
return new BadRequestException(err.message);
default:
return err;
}
}
}
20 changes: 20 additions & 0 deletions src/core/test/router/routes-resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Controller } from '../../../common/decorators/core/controller.decorator
import { RequestMapping } from '../../../common/decorators/http/request-mapping.decorator';
import { RequestMethod } from '../../../common/enums/request-method.enum';
import { ApplicationConfig } from '../../application-config';
import { BadRequestException } from '@nestjs/common';

describe('RoutesResolver', () => {
@Controller('global')
Expand Down Expand Up @@ -73,4 +74,23 @@ describe('RoutesResolver', () => {
expect(spy.calledTwice).to.be.true;
});
});

describe('mapExternalExceptions', () => {
describe('when exception prototype is', () => {
describe('SyntaxError', () => {
it('should map to BadRequestException', () => {
const err = new SyntaxError();
const outputErr = routesResolver.mapExternalException(err);
expect(outputErr).to.be.instanceof(BadRequestException);
});
});
describe('other', () => {
it('should behave as an identity', () => {
const err = new Error();
const outputErr = routesResolver.mapExternalException(err);
expect(outputErr).to.be.eql(err);
});
});
});
});
});

0 comments on commit cfa4bff

Please sign in to comment.