AboutBlogNewsletterLinks

TIL: Getting HTTP Status Codes from Flask errorhandler

Published 2020-07-24 by Seth Larson
Reading time: 1 minute

When you're using Flasks errorhandler decorator for a specific HTTP status code it's clear what HTTP status code the response will be:

from flask import Flask

app = Flask(__name__)

@app.errorhandler(404)
def on_not_found(error):
    return "This is a 404 for sure!", 404

However I didn't know how to write an error handler that handles all HTTP status calls, abort(XYZ) calls, and just general exceptions being raised from the stack. This is what I ended up with:

from flask import Flask

app = Flask(__name__)

@app.errorhandler(Exception)
def on_error(error):
    return "This is an error!", ... # <- What do I put here?

but I wasn't sure how to get the status code from the HTTPException that Flask was going to raise (another thing to note is that HTTPException is from werkzeug, not from Flask). I tried "status_code" and "status", to no avail and then ran dir() on the error which revealed "code" as the property to use. This was my final function without a ton of guards since it's for a quick project:

from flask import Flask

app = Flask(__name__)

@app.errorhandler(Exception)
def on_error(error):
    # Errors raised from places besides abort()
    # or routing failures will have a status code
    # of 500 for internal error.
    status_code = getattr(error, "code", 500)
    return f"HTTP Error {status_code}", status_code

Thanks for reading! ♡ Did you find this article helpful and want more content like it? Get notified of new posts by subscribing to the RSS feed or the email newsletter.


This work is licensed under CC BY-SA 4.0