Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

python - How can I implement a custom error handler for all HTTP errors in Flask?

In my Flask app, I can easily expand the list of errors handled by a single custom error handler by adding errorhandler decorators for each error code as with

@application.errorhandler(404)
@application.errorhandler(401)
@application.errorhandler(500)
def http_error_handler(error):
    return flask.render_template('error.html', error=error), error.code

However this approach requires an explicit decorator for each error code. Is there a way decorate my (single) http_error_handler function so that it handles all HTTP errors?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the errorhandler decorator with an exception class rather than an error code as an argument, as is described here. Thus you could try for instance

@application.errorhandler(HTTPException)
def http_error_handler(error):

to handle all HTTP errors (which presumably means all HTTP error codes), or even

@application.errorhandler(Exception)
def http_error_handler(error):

to handle all uncaught exceptions

Edit: Having looked at the flask source code, there is a 'TRAP_HTTP_EXCEPTIONS' flag in the app config, which you can change (by doing for instance app.config['TRAP_HTTP_EXCEPTIONS']=True).

(Roughly) When this flag is false, exceptions which are instances of HTTPException are handled by the functions you've decorated with errorhandler(n) where n is an HTTP error code; and when this flag is true, all instances of HTTPException are instead handled by the functions you've decorated with errorhandler(c), where c is an exception class.

Thus doing

app.config['TRAP_HTTP_EXCEPTIONS']=True

@application.errorhandler(Exception)
def http_error_handler(error):

should achieve what you want.

Since it looks like HTTPException has subclasses for each HTTP error code (see here), setting 'TRAP_HTTP_EXCEPTIONS' and decorating your error handlers with exception classes not error codes looks like a strictly more flexible way of doing things.

For reference, my flask error handling now looks like:

app.config['TRAP_HTTP_EXCEPTIONS']=True

@app.errorhandler(Exception)
def handle_error(e):
    try:
        if e.code < 400:
            return flask.Response.force_type(e, flask.request.environ)
        elif e.code == 404:
            return make_error_page("Page Not Found", "The page you're looking for was not found"), 404
        raise e
    except:
        return make_error_page("Error", "Something went wrong"), 500

This does everything I want, and seems to handle all errors, both HTTP and internal. The if e.code < 400 bit is there to use flask's default behaviour for redirects and the like (otherwise those end up as error 500s, which isn't what you want)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...