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

Wow, you made it to the end!

If you're like me, you don't believe social media should be the way to get updates on the cool stuff your friends are up to. Instead, you should either follow my blog with the RSS reader of your choice or via my email newsletter for guaranteed article publication notifications.

If you really enjoyed a piece I would be grateful if you shared with a friend. If you have follow-up thoughts you can send them via email.

Thanks for reading!
— Seth