test_user_error_handler.py
This file
Path
tests/test_user_error_handler.py
281 lines · 7.8 KiB · Python
Last changed by
remove unused module docstringsDavid Lordbefore adoptionf2f027d
History of this fileTake it
The walk stopped after 200 commits: lines older than e102e5c point at it.
Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
line 1why, one step back1import pytestMerge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
lines 2–5why, one step back2from werkzeug.exceptions import Forbidden3from werkzeug.exceptions import HTTPException4from werkzeug.exceptions import InternalServerError5from werkzeug.exceptions import NotFoundMerge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 6–16why, one step back67import flask8910def test_error_handler_no_match(app, client):11 class CustomException(Exception):12 pass1314 @app.errorhandler(CustomException)15 def custom_exception_handler(e):16 assert isinstance(e, CustomException)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 17why, one step back17 return "custom"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 18–20why, one step back1819 @app.errorhandler(500)20 def handle_500(e):Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 21–22why, one step back21 assert isinstance(e, InternalServerError)22 original = getattr(e, "original_exception", None)Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 23why, one step back23Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
line 24why, one step back24 if original is not None:Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 25why, one step back25 return f"wrapped {type(original).__name__}"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 26–28why, one step back2627 return "direct"28Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 29why, one step back29 @app.route("/custom")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 30–32why, one step back30 def custom_test():31 raise CustomException()32Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 33why, one step back33 @app.route("/keyerror")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 34–36why, one step back34 def key_error():35 raise KeyError()36Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 37–40why, one step back37 @app.route("/abort")38 def do_abort():39 flask.abort(500)40Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 41why, one step back41 app.testing = FalseMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 42why, one step back42 assert client.get("/custom").data == b"custom"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 43–44why, one step back43 assert client.get("/keyerror").data == b"wrapped KeyError"44 assert client.get("/abort").data == b"direct"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 45–59why, one step back454647def test_error_handler_subclass(app):48 class ParentException(Exception):49 pass5051 class ChildExceptionUnregistered(ParentException):52 pass5354 class ChildExceptionRegistered(ParentException):55 pass5657 @app.errorhandler(ParentException)58 def parent_exception_handler(e):59 assert isinstance(e, ParentException)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 60why, one step back60 return "parent"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 61–64why, one step back6162 @app.errorhandler(ChildExceptionRegistered)63 def child_exception_handler(e):64 assert isinstance(e, ChildExceptionRegistered)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 65why, one step back65 return "child-registered"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 66why, one step back66Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 67why, one step back67 @app.route("/parent")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 68–70why, one step back68 def parent_test():69 raise ParentException()70Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 71why, one step back71 @app.route("/child-unregistered")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 72–74why, one step back72 def unregistered_test():73 raise ChildExceptionUnregistered()74Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 75why, one step back75 @app.route("/child-registered")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 76–80why, one step back76 def registered_test():77 raise ChildExceptionRegistered()7879 c = app.test_client()80Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 81–83why, one step back81 assert c.get("/parent").data == b"parent"82 assert c.get("/child-unregistered").data == b"parent"83 assert c.get("/child-registered").data == b"child-registered"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 84–95why, one step back848586def test_error_handler_http_subclass(app):87 class ForbiddenSubclassRegistered(Forbidden):88 pass8990 class ForbiddenSubclassUnregistered(Forbidden):91 pass9293 @app.errorhandler(403)94 def code_exception_handler(e):95 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 96why, one step back96 return "forbidden"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 97–100why, one step back9798 @app.errorhandler(ForbiddenSubclassRegistered)99 def subclass_exception_handler(e):100 assert isinstance(e, ForbiddenSubclassRegistered)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 101why, one step back101 return "forbidden-registered"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 102why, one step back102Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 103why, one step back103 @app.route("/forbidden")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 104–106why, one step back104 def forbidden_test():105 raise Forbidden()106Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 107why, one step back107 @app.route("/forbidden-registered")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 108–110why, one step back108 def registered_test():109 raise ForbiddenSubclassRegistered()110Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 111why, one step back111 @app.route("/forbidden-unregistered")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 112–116why, one step back112 def unregistered_test():113 raise ForbiddenSubclassUnregistered()114115 c = app.test_client()116Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 117–119why, one step back117 assert c.get("/forbidden").data == b"forbidden"118 assert c.get("/forbidden-unregistered").data == b"forbidden"119 assert c.get("/forbidden-registered").data == b"forbidden-registered"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 120–122why, one step back120121122def test_error_handler_blueprint(app):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 123why, one step back123 bp = flask.Blueprint("bp", __name__)Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 124–126why, one step back124125 @bp.errorhandler(500)126 def bp_exception_handler(e):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 127why, one step back127 return "bp-error"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 128why, one step back128Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 129why, one step back129 @bp.route("/error")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 130–134why, one step back130 def bp_test():131 raise InternalServerError()132133 @app.errorhandler(500)134 def app_exception_handler(e):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 135why, one step back135 return "app-error"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 136why, one step back136Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 137why, one step back137 @app.route("/error")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 138–140why, one step back138 def app_test():139 raise InternalServerError()140Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 141why, one step back141 app.register_blueprint(bp, url_prefix="/bp")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 142–144why, one step back142143 c = app.test_client()144Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 145–146why, one step back145 assert c.get("/error").data == b"app-error"146 assert c.get("/bp/error").data == b"bp-error"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 147–149why, one step back147148149def test_default_error_handler():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 150why, one step back150 bp = flask.Blueprint("bp", __name__)Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 151–155why, one step back151152 @bp.errorhandler(HTTPException)153 def bp_exception_handler(e):154 assert isinstance(e, HTTPException)155 assert isinstance(e, NotFound)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 156why, one step back156 return "bp-default"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 157–158why, one step back157158 @bp.errorhandler(Forbidden)Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86
line 159why, one step back159 def bp_forbidden_handler(e):Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 160why, one step back160 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 161why, one step back161 return "bp-forbidden"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 162why, one step back162Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 163why, one step back163 @bp.route("/undefined")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 164–166why, one step back164 def bp_registered_test():165 raise NotFound()166Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 167why, one step back167 @bp.route("/forbidden")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 168–173why, one step back168 def bp_forbidden_test():169 raise Forbidden()170171 app = flask.Flask(__name__)172173 @app.errorhandler(HTTPException)Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86
line 174why, one step back174 def catchall_exception_handler(e):Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 175–176why, one step back175 assert isinstance(e, HTTPException)176 assert isinstance(e, NotFound)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 177why, one step back177 return "default"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 178–179why, one step back178179 @app.errorhandler(Forbidden)Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86
line 180why, one step back180 def catchall_forbidden_handler(e):Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 181why, one step back181 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 182why, one step back182 return "forbidden"Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
line 183why, one step back183Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 184why, one step back184 @app.route("/forbidden")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 185–187why, one step back185 def forbidden():186 raise Forbidden()187Merge remote-tracking branch 'origin/1.0-maintenance'David Lordbefore adoptiona74864e
lines 188–191why, one step back188 @app.route("/slash/")189 def slash():190 return "slash"191Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 192why, one step back192 app.register_blueprint(bp, url_prefix="/bp")Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c
lines 193–194why, one step back193194 c = app.test_client()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 195–198why, one step back195 assert c.get("/bp/undefined").data == b"bp-default"196 assert c.get("/bp/forbidden").data == b"bp-forbidden"197 assert c.get("/undefined").data == b"default"198 assert c.get("/forbidden").data == b"forbidden"Merge remote-tracking branch 'origin/1.0-maintenance'David Lordbefore adoptiona74864e
lines 199–200why, one step back199 # Don't handle RequestRedirect raised when adding slash.200 assert c.get("/slash", follow_redirects=True).data == b"slash"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 201–202why, one step back201202Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 203why, one step back203class TestGenericHandlers:Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 204–233why, one step back204 """Test how very generic handlers are dispatched to."""205206 class Custom(Exception):207 pass208209 @pytest.fixture()210 def app(self, app):211 @app.route("/custom")212 def do_custom():213 raise self.Custom()214215 @app.route("/error")216 def do_error():217 raise KeyError()218219 @app.route("/abort")220 def do_abort():221 flask.abort(500)222223 @app.route("/raise")224 def do_raise():225 raise InternalServerError()226227 app.config["PROPAGATE_EXCEPTIONS"] = False228 return app229230 def report_error(self, e):231 original = getattr(e, "original_exception", None)232233 if original is not None:Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 234why, one step back234 return f"wrapped {type(original).__name__}"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
line 235why, one step back235Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 236why, one step back236 return f"direct {type(e).__name__}"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 237–281why, one step back237238 @pytest.mark.parametrize("to_handle", (InternalServerError, 500))239 def test_handle_class_or_code(self, app, client, to_handle):240 """``InternalServerError`` and ``500`` are aliases, they should241 have the same behavior. Both should only receive242 ``InternalServerError``, which might wrap another error.243 """244245 @app.errorhandler(to_handle)246 def handle_500(e):247 assert isinstance(e, InternalServerError)248 return self.report_error(e)249250 assert client.get("/custom").data == b"wrapped Custom"251 assert client.get("/error").data == b"wrapped KeyError"252 assert client.get("/abort").data == b"direct InternalServerError"253 assert client.get("/raise").data == b"direct InternalServerError"254255 def test_handle_generic_http(self, app, client):256 """``HTTPException`` should only receive ``HTTPException``257 subclasses. It will receive ``404`` routing exceptions.258 """259260 @app.errorhandler(HTTPException)261 def handle_http(e):262 assert isinstance(e, HTTPException)263 return str(e.code)264265 assert client.get("/error").data == b"500"266 assert client.get("/abort").data == b"500"267 assert client.get("/not-found").data == b"404"268269 def test_handle_generic(self, app, client):270 """Generic ``Exception`` will handle all exceptions directly,271 including ``HTTPExceptions``.272 """273274 @app.errorhandler(Exception)275 def handle_exception(e):276 return self.report_error(e)277278 assert client.get("/custom").data == b"direct Custom"279 assert client.get("/error").data == b"direct KeyError"280 assert client.get("/abort").data == b"direct InternalServerError"281 assert client.get("/not-found").data == b"direct NotFound"