You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Registering a handler for HTTPException has no effect #941
When requesting a page for which no route exists, a JSON response should be returned by the error handler, but instead, the usual Flask-generated HTTP error page is returned.
On the other hand, if the error handler is defined to handle a specific error code (by passing the error code to the app.errorhandler decorator), the exception is trapped and the JSON message returned.
As wekzeug.exceptions.HTTPException is the class raised internally by the abort() function, why isn't it possible to create a "catch-all" handler like this? Am I missing something?
Do you know where the bug exactly lies? If this is an already known bug, with some informations about it maybe I could help by trying to fix it by myself and then submit a pull-request.
$ venv/bin/python app.py
F.
======================================================================
FAIL: test_http_handler (__main__.ErrorHandlerTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "app.py", line 35, in test_http_handler
self.assertEqual(rv.data, 'generic')
AssertionError: '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>500 ...'!= 'generic'
----------------------------------------------------------------------
Ran 2 tests in 0.021s
FAILED (failures=1)
Code 500 is an InternalServerError (subclass of HTTPException). Since there is no specific handler for it (unlike NotFound), one would hope it would be handled by @app.errorhandler(HTTPException)
so the legal classes to register handlers for: HTTPException, the finite set of all subclasses of it (synonymous with error codes), and subclasses of subclasses (e.g. class ForbiddenBecauseNotRegistered(Forbidden): ...
and the legal instances of exceptions to raise are instances of subclasses of HTTPException but not direct instances of HTTPException.
Reacted by John Chang, Dries007, Naresh, Andrei Ivanov, Jérôme Lafréchoux, Shane, Mark E. Haase, Jean Landercy, Antony Gelberg, Michael Karr and 1 more
@Bekt I'm a bit late to the party, hope you're around... I'm trying to get flask to return JSON errors when there are exceptions. Your code works great except for raise error.get_response(). According to werkzeug docs:
If you call get_response() on an exception you will get back a regular BaseResponse object, even if you are using a custom subclass.
This results in raise error.get_response() returning TypeError: exceptions must derive from BaseException. Did I miss something? Is there a better way of doing this in 2019 as opposed to 2015.
I mean, the error message is pretty clear: error.get_response returns a Response, not an Exception, so you probably just have to replace raise with return
Long story short: It is a bug, it's a flaw in the design, it's easy to work around as a user, so it's rather low priority to fix. :S
Hi
So late, but it is NOT a bug, this is a design decision. HTTPException is the base type for all http exceptions in werkzeug.
and werkzeug or flask themselve, never raise an instance of HTTPException, instead they raise different subclasses of it.
so this is implemented this way to enforce that HTTPException is the base type, and should not be raised directly. but you
could register an error handler for HTTPException and handle all subclasses of it without any problem. the only thing
that you have to do is NOT to raise a direct instance of HTTPException, instead raise its subclasses.
Activity
untitaker commentedon Jan 6, 2014
Long story short: It is a bug, it's a flaw in the design, it's easy to work around as a user, so it's rather low priority to fix. :S
mdeous commentedon Jan 7, 2014
Do you know where the bug exactly lies? If this is an already known bug, with some informations about it maybe I could help by trying to fix it by myself and then submit a pull-request.
untitaker commentedon Jan 8, 2014
I think it would require rewriting most of the error handling system for Flask, the code for this is in
flask/app.pyFix pallets#941 -- allow errorhandlers for HTTPException
Fix pallets#941 -- allow errorhandlers for HTTPException
untitaker commentedon Jan 14, 2014
#952 is a possible solution to this.
Fix pallets#941 -- allow errorhandlers for HTTPException
untitaker commentedon Oct 19, 2014
Fixed by #839
Bekt commentedon Jul 5, 2015
#839 doesn't fix the issue OP posted -- it only handles subclasses of
HTTPException.untitaker commentedon Jul 5, 2015
Could you provide a testcase that fails?
Bekt commentedon Jul 5, 2015
Code
500is anInternalServerError(subclass ofHTTPException). Since there is no specific handler for it (unlikeNotFound), one would hope it would be handled by@app.errorhandler(HTTPException)untitaker commentedon Jul 5, 2015
Ahh, I think this is because HTTPException doesn't have a HTTP code, but the error handling logic tries to find an errorhandler with the HTTP code.
cc @flying-sheep
3 remaining items
flying-sheep commentedon Jul 6, 2015
right, i didn’t think of this, sorry!
so the legal classes to register handlers for:
HTTPException, the finite set of all subclasses of it (synonymous with error codes), and subclasses of subclasses (e.g.class ForbiddenBecauseNotRegistered(Forbidden): ...and the legal instances of exceptions to raise are instances of subclasses of
HTTPExceptionbut not direct instances ofHTTPException.this means the following assumptions hold:
we should add a way to add a handler for
HTTPExceptions without allowing users to raise an instance ofHTTPException.flying-sheep commentedon Jul 6, 2015
@Bekt that PR should be closed, it was written against the old broken behavior that I replaced.
Bekt commentedon Jul 6, 2015
For those who simply want to override the default behavior of the default
HTTPExceptionsubclasses, you can do something like this:cerickson commentedon May 22, 2017
#2144 seems to solve this issue
Added support for generic HTTPException handlers on app and blueprints
Added support for generic HTTPException handlers on app and blueprints
Added support for generic HTTPException handlers on app and blueprints
jeffwidman commentedon May 23, 2017
Resolved by #2314
antgel commentedon Nov 9, 2019
@Bekt I'm a bit late to the party, hope you're around... I'm trying to get flask to return JSON errors when there are exceptions. Your code works great except for
raise error.get_response(). According to werkzeug docs:This results in
raise error.get_response()returningTypeError: exceptions must derive from BaseException. Did I miss something? Is there a better way of doing this in 2019 as opposed to 2015.Bekt commentedon Nov 12, 2019
@antgel i would be lying if i said i remember even remotely what this thread is about... i miss my flask days.
flying-sheep commentedon Nov 12, 2019
I mean, the error message is pretty clear:
error.get_responsereturns a Response, not an Exception, so you probably just have to replaceraisewithreturnmononobi commentedon Oct 4, 2020
Hi
So late, but it is NOT a bug, this is a design decision. HTTPException is the base type for all http exceptions in werkzeug.
and werkzeug or flask themselve, never raise an instance of HTTPException, instead they raise different subclasses of it.
so this is implemented this way to enforce that HTTPException is the base type, and should not be raised directly. but you
could register an error handler for HTTPException and handle all subclasses of it without any problem. the only thing
that you have to do is NOT to raise a direct instance of HTTPException, instead raise its subclasses.