Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Exception filter] Use default exception handler on condition #860

Closed
uiii opened this issue Jul 10, 2018 · 4 comments
Closed

[Exception filter] Use default exception handler on condition #860

uiii opened this issue Jul 10, 2018 · 4 comments

Comments

@uiii
Copy link

uiii commented Jul 10, 2018

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.

@quezak
Copy link

quezak commented Jul 18, 2018

@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)?

@kamilmysliwiec
Copy link
Member

resolved by #908

kamilmysliwiec added a commit that referenced this issue Aug 1, 2018
@kamilmysliwiec
Copy link
Member

5.2.0 is here!

@lock
Copy link

lock bot commented Sep 24, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants