Nest: [Exception filter] Use default exception handler on condition

Created on 10 Jul 2018  路  4Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


There is no way to pass the execution to the default exception handler on some condition when using custom exception filter.

Expected behavior


E.g. when you return false from catch method of your custom exception filter, the default exception handler should proceed the execution as if there is no custom exception filter.

What is the motivation / use case for changing the behavior?


I'm creating single page application and want to return an index.html file on NotFoundException, but only if the requested path doesn't start with /api, in this case I would like to handle the exception the default way.

core done 馃憦 type

Most helpful comment

5.2.0 is here!

All 4 comments

@kamilmysliwiec I have a question that I think is related to this (if not, please tell me and I'll post a separate issue).

TL;DR: I wanted to use exception filters to just map some internal exceptions to external user-friently HttpExceptions, and let Nest handle them the usual way -- but if I do that, the new exceptions are not handled by Nest at all.

Example: a few of my services use TypeORM's findOneOrFail, which throws an EntityNotFoundError on failure. On internal calls from other modules, I most often want to handle this exception in some way, but if it happens directly in a controller, I want to catch it and throw a NotFoundException to have a correct response status code. So I've made this little filter, hoping it will do the job:

@Catch(EntityNotFoundError)
export class EntityNotFoundFilter implements ExceptionFilter {
    catch(exception: EntityNotFoundError, _host: ArgumentsHost) {
        throw new NotFoundException(exception.message);
    }
}

Current behavior: The new, mapped error is not handled by the "built-in" exception handler, which transforms HttpExceptions to meaningful responses; instead nest fails badly -- there is an unhandled exception logged, and no response at all on the client side (not even the usual 500 internal server error):

 (node:3065) UnhandledPromiseRejectionWarning: Error: [object Object]
    at EntityNotFoundFilter.catch ([...]/errors.ts:32:15)
    at ExceptionsHandler.invokeCustomFilters ([...]/node_modules/@nestjs/core/exceptions/exceptions-handler.js:49:26)
     at ExceptionsHandler.next ([...]/node_modules/@nestjs/core/exceptions/exceptions-handler.js:13:18)
     at [...]/node_modules/@nestjs/core/router/router-proxy.js:12:35
     at <anonymous>
     at process._tickCallback (internal/process/next_tick.js:182:7)
 (node:3065) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)

Expected behavior: the thrown HttpException is handled as usual, producing a 404 NOT FOUND response with the requested message.

Question: is there some recommended way to achieve this, without manually constructing the response in my custom exception handler (or some other place)?

resolved by #908

5.2.0 is here!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marshall007 picture marshall007  路  3Comments

cojack picture cojack  路  3Comments

breitsmiley picture breitsmiley  路  3Comments

janckerchen picture janckerchen  路  3Comments

VRspace4 picture VRspace4  路  3Comments