test_user_error_handler.py
This file
Path
tests/test_user_error_handler.py
288 lines · 7.9 KiB · Python
Last changed by
apply pyupgradeDavid Lordbefore adoption524fd0b
History of this fileTake it
The walk stopped after 200 commits: lines older than e102e5c point at it.
Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 1–4why, one step back1"""2tests.test_user_error_handler3~~~~~~~~~~~~~~~~~~~~~~~~~~~~~4Merge branch '1.0.x'David Lordbefore adoption1351d0a
lines 5–6why, one step back5:copyright: 2010 Pallets6:license: BSD-3-ClauseMerge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 7why, one step back7"""Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
line 8why, one step back8import pytestMerge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
lines 9–12why, one step back9from werkzeug.exceptions import Forbidden10from werkzeug.exceptions import HTTPException11from werkzeug.exceptions import InternalServerError12from werkzeug.exceptions import NotFoundMerge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 13–23why, one step back1314import flask151617def test_error_handler_no_match(app, client):18 class CustomException(Exception):19 pass2021 @app.errorhandler(CustomException)22 def custom_exception_handler(e):23 assert isinstance(e, CustomException)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 24why, one step back24 return "custom"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 25–27why, one step back2526 @app.errorhandler(500)27 def handle_500(e):Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 28–29why, one step back28 assert isinstance(e, InternalServerError)29 original = getattr(e, "original_exception", None)Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 30why, one step back30Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 31–35why, one step back31 if original is not None:32 return "wrapped " + type(original).__name__3334 return "direct"35Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 36why, one step back36 @app.route("/custom")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 37–39why, one step back37 def custom_test():38 raise CustomException()39Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 40why, one step back40 @app.route("/keyerror")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 41–43why, one step back41 def key_error():42 raise KeyError()43Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 44–47why, one step back44 @app.route("/abort")45 def do_abort():46 flask.abort(500)47Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 48why, one step back48 app.testing = FalseMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 49why, one step back49 assert client.get("/custom").data == b"custom"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 50–51why, one step back50 assert client.get("/keyerror").data == b"wrapped KeyError"51 assert client.get("/abort").data == b"direct"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 52–66why, one step back525354def test_error_handler_subclass(app):55 class ParentException(Exception):56 pass5758 class ChildExceptionUnregistered(ParentException):59 pass6061 class ChildExceptionRegistered(ParentException):62 pass6364 @app.errorhandler(ParentException)65 def parent_exception_handler(e):66 assert isinstance(e, ParentException)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 67why, one step back67 return "parent"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 68–71why, one step back6869 @app.errorhandler(ChildExceptionRegistered)70 def child_exception_handler(e):71 assert isinstance(e, ChildExceptionRegistered)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 72why, one step back72 return "child-registered"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 73why, one step back73Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 74why, one step back74 @app.route("/parent")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 75–77why, one step back75 def parent_test():76 raise ParentException()77Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 78why, one step back78 @app.route("/child-unregistered")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 79–81why, one step back79 def unregistered_test():80 raise ChildExceptionUnregistered()81Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 82why, one step back82 @app.route("/child-registered")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 83–87why, one step back83 def registered_test():84 raise ChildExceptionRegistered()8586 c = app.test_client()87Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 88–90why, one step back88 assert c.get("/parent").data == b"parent"89 assert c.get("/child-unregistered").data == b"parent"90 assert c.get("/child-registered").data == b"child-registered"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 91–102why, one step back919293def test_error_handler_http_subclass(app):94 class ForbiddenSubclassRegistered(Forbidden):95 pass9697 class ForbiddenSubclassUnregistered(Forbidden):98 pass99100 @app.errorhandler(403)101 def code_exception_handler(e):102 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 103why, one step back103 return "forbidden"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 104–107why, one step back104105 @app.errorhandler(ForbiddenSubclassRegistered)106 def subclass_exception_handler(e):107 assert isinstance(e, ForbiddenSubclassRegistered)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 108why, one step back108 return "forbidden-registered"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 109why, one step back109Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 110why, one step back110 @app.route("/forbidden")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 111–113why, one step back111 def forbidden_test():112 raise Forbidden()113Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 114why, one step back114 @app.route("/forbidden-registered")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 115–117why, one step back115 def registered_test():116 raise ForbiddenSubclassRegistered()117Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 118why, one step back118 @app.route("/forbidden-unregistered")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 119–123why, one step back119 def unregistered_test():120 raise ForbiddenSubclassUnregistered()121122 c = app.test_client()123Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 124–126why, one step back124 assert c.get("/forbidden").data == b"forbidden"125 assert c.get("/forbidden-unregistered").data == b"forbidden"126 assert c.get("/forbidden-registered").data == b"forbidden-registered"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 127–129why, one step back127128129def test_error_handler_blueprint(app):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 130why, one step back130 bp = flask.Blueprint("bp", __name__)Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 131–133why, one step back131132 @bp.errorhandler(500)133 def bp_exception_handler(e):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 134why, one step back134 return "bp-error"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 135why, one step back135Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 136why, one step back136 @bp.route("/error")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 137–141why, one step back137 def bp_test():138 raise InternalServerError()139140 @app.errorhandler(500)141 def app_exception_handler(e):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 142why, one step back142 return "app-error"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 143why, one step back143Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 144why, one step back144 @app.route("/error")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 145–147why, one step back145 def app_test():146 raise InternalServerError()147Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 148why, one step back148 app.register_blueprint(bp, url_prefix="/bp")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 149–151why, one step back149150 c = app.test_client()151Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 152–153why, one step back152 assert c.get("/error").data == b"app-error"153 assert c.get("/bp/error").data == b"bp-error"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 154–156why, one step back154155156def test_default_error_handler():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 157why, one step back157 bp = flask.Blueprint("bp", __name__)Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 158–162why, one step back158159 @bp.errorhandler(HTTPException)160 def bp_exception_handler(e):161 assert isinstance(e, HTTPException)162 assert isinstance(e, NotFound)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 163why, one step back163 return "bp-default"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 164–165why, one step back164165 @bp.errorhandler(Forbidden)Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86
line 166why, one step back166 def bp_forbidden_handler(e):Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 167why, one step back167 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 168why, one step back168 return "bp-forbidden"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 169why, one step back169Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 170why, one step back170 @bp.route("/undefined")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 171–173why, one step back171 def bp_registered_test():172 raise NotFound()173Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 174why, one step back174 @bp.route("/forbidden")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 175–180why, one step back175 def bp_forbidden_test():176 raise Forbidden()177178 app = flask.Flask(__name__)179180 @app.errorhandler(HTTPException)Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86
line 181why, one step back181 def catchall_exception_handler(e):Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 182–183why, one step back182 assert isinstance(e, HTTPException)183 assert isinstance(e, NotFound)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 184why, one step back184 return "default"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 185–186why, one step back185186 @app.errorhandler(Forbidden)Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86
line 187why, one step back187 def catchall_forbidden_handler(e):Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 188why, one step back188 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 189why, one step back189 return "forbidden"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 190why, one step back190Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 191why, one step back191 @app.route("/forbidden")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 192–194why, one step back192 def forbidden():193 raise Forbidden()194Merge remote-tracking branch 'origin/1.0-maintenance'David Lordbefore adoptiona74864e
lines 195–198why, one step back195 @app.route("/slash/")196 def slash():197 return "slash"198Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 199why, one step back199 app.register_blueprint(bp, url_prefix="/bp")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 200–201why, one step back200201 c = app.test_client()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 202–205why, one step back202 assert c.get("/bp/undefined").data == b"bp-default"203 assert c.get("/bp/forbidden").data == b"bp-forbidden"204 assert c.get("/undefined").data == b"default"205 assert c.get("/forbidden").data == b"forbidden"Merge remote-tracking branch 'origin/1.0-maintenance'David Lordbefore adoptiona74864e
lines 206–207why, one step back206 # Don't handle RequestRedirect raised when adding slash.207 assert c.get("/slash", follow_redirects=True).data == b"slash"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 208–209why, one step back208209apply pyupgradeDavid Lordbefore adoption524fd0b
line 210why, one step back210class TestGenericHandlers:Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 211–288why, one step back211 """Test how very generic handlers are dispatched to."""212213 class Custom(Exception):214 pass215216 @pytest.fixture()217 def app(self, app):218 @app.route("/custom")219 def do_custom():220 raise self.Custom()221222 @app.route("/error")223 def do_error():224 raise KeyError()225226 @app.route("/abort")227 def do_abort():228 flask.abort(500)229230 @app.route("/raise")231 def do_raise():232 raise InternalServerError()233234 app.config["PROPAGATE_EXCEPTIONS"] = False235 return app236237 def report_error(self, e):238 original = getattr(e, "original_exception", None)239240 if original is not None:241 return "wrapped " + type(original).__name__242243 return "direct " + type(e).__name__244245 @pytest.mark.parametrize("to_handle", (InternalServerError, 500))246 def test_handle_class_or_code(self, app, client, to_handle):247 """``InternalServerError`` and ``500`` are aliases, they should248 have the same behavior. Both should only receive249 ``InternalServerError``, which might wrap another error.250 """251252 @app.errorhandler(to_handle)253 def handle_500(e):254 assert isinstance(e, InternalServerError)255 return self.report_error(e)256257 assert client.get("/custom").data == b"wrapped Custom"258 assert client.get("/error").data == b"wrapped KeyError"259 assert client.get("/abort").data == b"direct InternalServerError"260 assert client.get("/raise").data == b"direct InternalServerError"261262 def test_handle_generic_http(self, app, client):263 """``HTTPException`` should only receive ``HTTPException``264 subclasses. It will receive ``404`` routing exceptions.265 """266267 @app.errorhandler(HTTPException)268 def handle_http(e):269 assert isinstance(e, HTTPException)270 return str(e.code)271272 assert client.get("/error").data == b"500"273 assert client.get("/abort").data == b"500"274 assert client.get("/not-found").data == b"404"275276 def test_handle_generic(self, app, client):277 """Generic ``Exception`` will handle all exceptions directly,278 including ``HTTPExceptions``.279 """280281 @app.errorhandler(Exception)282 def handle_exception(e):283 return self.report_error(e)284285 assert client.get("/custom").data == b"direct Custom"286 assert client.get("/error").data == b"direct KeyError"287 assert client.get("/abort").data == b"direct InternalServerError"288 assert client.get("/not-found").data == b"direct NotFound"