Skip to content

Instantly share code, notes, and snippets.

@tehasdf
Last active December 12, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tehasdf/4749354 to your computer and use it in GitHub Desktop.
Save tehasdf/4749354 to your computer and use it in GitHub Desktop.
throwing errors from within errorhandlers with flask
from flask import Flask, abort
app = Flask(__name__)
@app.route('/')
def foo():
abort(403)
return 'this wont ever be returned'
@app.errorhandler(403)
def foo(x):
# here we throw an exception inside an error handler
abort(404)
return 'this wont ever be returned'
@app.errorhandler(404)
def foo(x):
return 'im the 404 handler'
@app.errorhandler(500)
def foo(err):
# check here for a 500 to avoid an infinite loop
return app.handle_user_exception(err)
with app.test_client() as c:
assert c.get('/').data == 'im the 404 handler'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment