-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
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
Setting error handler for unknown code fails #1837
Comments
I have the same issue. |
I can see that this might be annoying. We need to figure out how to deal with non standard status codes and error handlers. How did you use this with Flask < 0.11? |
Here is an example of my approach: https://github.com/hda-technical/dancebooks/blob/master/www/main.py#L488 I just set custom exception handler to each HTTP code from werkzeug.HTTP_STATUS_CODES. |
I am also having an issue with error handlers. I am using it slightly differently than above (https://github.com/pjuu/pjuu/blob/master/pjuu/lib/errors.py) to try make it as generic as possible. Not sure if my case is related but I am getting:
I can get around this I think by implementing the generic error handler as a class with a I can open a new issue if you want or if I'm just using undocumented APIs, please feel free to tell me to get lost 😈 |
@docapotamus You should register error handlers by the |
@sublee thanks for showing me the error of my ways. Will get this sorted it's now not a problem. Sorry to hijack this issue. |
There was an issue with the error handlers which I though was a Flask problem but it was me using the wrong way of attaching the custom error handler (pallets/flask#1837 (comment)) Also did a requirements updates: - codecov - sphinx - flask
Looks like setting error handlers by assigning to Solution: Use However, the documentation makes no mention that this no longer works (the latest docs still give an example of how to use |
Yes, sorry, the docs are completely out of date. The new mapping is |
Also I'd like to emphasize in the docs that it's discouraged to use this attribute. |
Re API improvements: How about making the actual map a private attribute and force access through dedicated APIs, such as Re documentation: The reason we were using the map in the first place was that using a decorator didn't work with application factories, and somehow we missed I'd be happy to help out with either docs or code changes, let me know your thoughts and what would be most useful. |
Regardless of the existence of the other function, this isn't true. A decorator is just a function that is passed a function, it can be used anywhere. def factory():
...
app.errorhandler(404)(our_404_handler) |
@davidism Sure. Though I suspect this usage isn't obvious to all (most?) users. Hence the existence of Just sent out a PR for improving some of the documentation: #2077. Happy to clean up the API reference docs, too, if this one's going in the right direction. @untitaker @mitsuhiko Curious what your thoughts are re my proposal for blocking access to |
Not a specialist of Flask code but did hit the same kind of issue, you can see gist of how we set error handling here (code has been cleaned a bit): https://gist.github.com/hrbonz/5cc9d9d1a63593cd87b3ef555470706c This used to work fine (in 0.10.1), the problem seems to be that now (I don't know how it was before), Flask._find_error_handler() will try to strictly match the error code from Should I create a factory to register every error code individually or is it a bug in how the handlers are found? Bottom line is, it looks like we now have to register all exception codes one by one instead of simply creating a generic error handler. |
Flask 0.11.1 crashes when using the obsolete app.error_handler_spec() method. Using app_register_error_handler() instead. See pallets/flask#1837 for details. Closes #904. Closes #945.
Also, trap_http_exception NO LONGER works. I don't know why you have to force people to register error handler by codes. For something simple like this:
which used to work, but now no longer works because of this forced look up in the dictionary by |
Not sure this is a correct way of handling this or not but it works...
The key is that default_exceptions is a dictionary of valid HTTP errors that werkzeug processes. I suppose you could add custom HTTP codes to this dictionary as well. The above returns a response like so:
|
This is still an issue, with this:
causes a crash:
In my case I want a global error handler, that catches everything to do the handling of errors myself. Workaround for me (why am I even using flask at this point):
Flask Version: 0.12.1 |
I'm looking into fixing this. @mitsuhiko what do you think about the app object holding a mapping from error codes to exceptions, where httpexception-subclasses are dynamically added if errorhandlers for unknown codes are registered? So basically |
Here's what I'm thinking: The Flask app has an instance of |
Right, that's better.
…On April 15, 2017 2:14:15 PM GMT+02:00, David Lord ***@***.***> wrote:
Here's what I'm thinking: The Flask app has an instance of
`werkzeug.exceptions.Aborter`. Custom exceptions get added to that
instances map. The Flask version of the abort `abort` tries
`current_app.aborter()` first (or only).
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#1837 (comment)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
This is a long discussion, to summarize:
|
The simplest solution is to subclass from werkzeug.exceptions import HTTPException
class FourZeroTwo(HTTPException):
code = 402
@app.errorhandler(FourZeroTwo)
def handle_402(e):
return '402', 402
@app.route('/')
def index():
raise FourZeroTwo() If you really want to use the code, it needs to be registered with Werkzeug in two places. from werkzeug.exceptions import default_exceptions, _aborter
default_exceptions[402] = FourZeroTwo
_aborter.mapping[402] = FourZeroTwo
app.errorhandler(402) # works
abort(402) # works |
@untitaker are you still looking into this? I'm not sure what a clean solution would look like. If all we have to go on is We could add a |
@georgthegreat I'm curious how you were aborting with that code. If you have a non-standard code that's not known to Werkzeug, create a subclass with the code and use that. If Werkzeug is missing a standard code, create a patch for Werkzeug to support it. Documenting this and closing. |
The following code:
raises
KeyError: 402
due to missing 402 in default exception. Code works fine with Flask=0.10.1The text was updated successfully, but these errors were encountered: