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

Catch-all errorhandler does not catch BadRequestKeyError #2268

Closed
isaac-peka opened this issue May 2, 2017 · 10 comments
Closed

Catch-all errorhandler does not catch BadRequestKeyError #2268

isaac-peka opened this issue May 2, 2017 · 10 comments

Comments

@isaac-peka
Copy link

isaac-peka commented May 2, 2017

Versions

Python 3.6.1
Flask 0.12.1
Werkzeug 0.12.1

Issue

A catch-all error handler (one that catchesException), does not catch werkzeug.exceptions.BadRequestKeyError. Here's a very quick illustration of the issue:

https://github.com/sampeka/flask-issue

Steps to repoduce:

  1. Clone the repo above, install requirements etc
  2. Run python ./example.py
  3. Navigate to /good, see that the error handler is correctly called
  4. Navigate to /bad, see that the error handler is not called

If I define an error handler specifically for BadRequestKeyError it will be called, as expected.

Edit: If you downgrade to Flask==0.10.1, this example will work.

@isaac-peka isaac-peka changed the title Catch-all errorhandler does not catch werkzeug.exceptions.BadRequestKeyError Catch-all errorhandler does not catch BadRequestKeyError May 2, 2017
@isaac-peka
Copy link
Author

isaac-peka commented May 2, 2017

Closing since this has the same issue: #2267.

We have a handler in another project for werkeug.exception.HTTPException which isn't called for BadRequestKeyError, despite it being a subclass.

@ThiefMaster
Copy link
Member

Does enabling the TRAP_BAD_REQUEST_ERRORS option help

@isaac-peka
Copy link
Author

@ThiefMaster no, still same issue

@isaac-peka
Copy link
Author

Actually, turns out the issue is with other werkzeug exceptions as well. Not sure if it's the same issue as the one I linked above, so reopening.

@isaac-peka isaac-peka reopened this May 2, 2017
@ngaya-ll
Copy link

ngaya-ll commented May 3, 2017

This is more similar to #941. This issue here is that HTTPExceptions are treated differently from other exceptions in the error handling code. Each HTTP status code gets its own handler map. So in order to catch BadRequestKeyError you would need to register a handler for the 404 code.

@Jane85
Copy link

Jane85 commented May 11, 2017

Is there still a way now to catch all HTTPExceptions? I believe that this is a good feature as we might use third party libraries and it is not always clear what kind of exceptions are thrown by the third party library.

@ngaya-ll
Copy link

ngaya-ll commented May 11, 2017

You can catch standard HTTPExceptions by iterating over werkzeug's standard error codes and registering a handler for each of them (can be the same handler):

from werkzeug.exceptions import default_exceptions
for code in default_exceptions:
    app.errorhandler(code)(handler)

This will work for most real-life cases. However, there are several bugs/caveats:

  • If an HTTPException is raised with a code attribute of None, it bypasses exception handling
  • Error handlers registered for integer codes will only catch HTTPExceptions with that code if the exception class inherits from werkzeug's default exception class for that code.
  • Custom HTTPExceptions with non-standard codes will not be caught.

You can change http exception handling by setting the TRAP_HTTP_EXCEPTIONS config parameter to True. With this configuration:

  • HTTPExceptions with a code of None will be handled like regular exceptions
  • HTTPExceptions with other code values are handled the same as usual (i.e. per-code error handler lookup), except that if no handler is found the exception will be re-raised instead of generating a default error page.

@Jane85
Copy link

Jane85 commented May 12, 2017

@ngaya-ll Hi, thanks for your quick reply! I tried the code that you suggested, however it does not take any effect. But I am using flask-restplus on top of flask, could it be that they have some conflicts?

@ngaya-ll
Copy link

I'm not familiar with flask-restplus but from the documentation it appears that that extension modifies the error handling behavior, so my above comment may not apply.

@davidism
Copy link
Member

Fixed by #2314.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants