test_user_error_handler.py
This file
Path
tests/test_user_error_handler.py
289 lines · 8.0 KiB · Python
Last changed by
Merge pull request #3574 from jackwardell/adding-error-message-to-assertDavid Lordbefore adoptiond091bb0
History of this fileTake it
The walk stopped after 200 commits: lines older than d7eea1b 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 NotFoundFix typo in issue templateGrey Libefore adoptiond7eea1b
lines 6–13why, one step back67import flask8910def test_error_handler_no_match(app, client):11 class CustomException(Exception):12 pass13Merge pull request #3574 from jackwardell/adding-error-message-to-assertDavid Lordbefore adoptiond091bb0
lines 14–16why, one step back14 class UnacceptableCustomException(BaseException):15 pass16Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 17–19why, one step back17 @app.errorhandler(CustomException)18 def custom_exception_handler(e):19 assert isinstance(e, CustomException)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 20why, one step back20 return "custom"Merge pull request #3574 from jackwardell/adding-error-message-to-assertDavid Lordbefore adoptiond091bb0
lines 21–25why, one step back2122 with pytest.raises(23 AssertionError, match="Custom exceptions must be subclasses of Exception."24 ):25 app.register_error_handler(UnacceptableCustomException, None)Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 26–28why, one step back2627 @app.errorhandler(500)28 def handle_500(e):Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 29–30why, one step back29 assert isinstance(e, InternalServerError)30 original = getattr(e, "original_exception", None)Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 31why, one step back31Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
line 32why, one step back32 if original is not None:Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 33why, one step back33 return f"wrapped {type(original).__name__}"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 34–36why, one step back3435 return "direct"36Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 37why, one step back37 @app.route("/custom")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 38–40why, one step back38 def custom_test():39 raise CustomException()40Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 41why, one step back41 @app.route("/keyerror")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 42–44why, one step back42 def key_error():43 raise KeyError()44Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 45–48why, one step back45 @app.route("/abort")46 def do_abort():47 flask.abort(500)48Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 49why, one step back49 app.testing = FalseMerge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 50why, one step back50 assert client.get("/custom").data == b"custom"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 51–52why, one step back51 assert client.get("/keyerror").data == b"wrapped KeyError"52 assert client.get("/abort").data == b"direct"Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 53–67why, one step back535455def test_error_handler_subclass(app):56 class ParentException(Exception):57 pass5859 class ChildExceptionUnregistered(ParentException):60 pass6162 class ChildExceptionRegistered(ParentException):63 pass6465 @app.errorhandler(ParentException)66 def parent_exception_handler(e):67 assert isinstance(e, ParentException)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 68why, one step back68 return "parent"Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 69–72why, one step back6970 @app.errorhandler(ChildExceptionRegistered)71 def child_exception_handler(e):72 assert isinstance(e, ChildExceptionRegistered)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 73why, one step back73 return "child-registered"Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 74why, one step back74Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 75why, one step back75 @app.route("/parent")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 76–78why, one step back76 def parent_test():77 raise ParentException()78Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 79why, one step back79 @app.route("/child-unregistered")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 80–82why, one step back80 def unregistered_test():81 raise ChildExceptionUnregistered()82Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 83why, one step back83 @app.route("/child-registered")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 84–88why, one step back84 def registered_test():85 raise ChildExceptionRegistered()8687 c = app.test_client()88Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 89–91why, one step back89 assert c.get("/parent").data == b"parent"90 assert c.get("/child-unregistered").data == b"parent"91 assert c.get("/child-registered").data == b"child-registered"Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 92–103why, one step back929394def test_error_handler_http_subclass(app):95 class ForbiddenSubclassRegistered(Forbidden):96 pass9798 class ForbiddenSubclassUnregistered(Forbidden):99 pass100101 @app.errorhandler(403)102 def code_exception_handler(e):103 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 104why, one step back104 return "forbidden"Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 105–108why, one step back105106 @app.errorhandler(ForbiddenSubclassRegistered)107 def subclass_exception_handler(e):108 assert isinstance(e, ForbiddenSubclassRegistered)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 109why, one step back109 return "forbidden-registered"Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 110why, one step back110Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 111why, one step back111 @app.route("/forbidden")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 112–114why, one step back112 def forbidden_test():113 raise Forbidden()114Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 115why, one step back115 @app.route("/forbidden-registered")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 116–118why, one step back116 def registered_test():117 raise ForbiddenSubclassRegistered()118Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 119why, one step back119 @app.route("/forbidden-unregistered")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 120–124why, one step back120 def unregistered_test():121 raise ForbiddenSubclassUnregistered()122123 c = app.test_client()124Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 125–127why, one step back125 assert c.get("/forbidden").data == b"forbidden"126 assert c.get("/forbidden-unregistered").data == b"forbidden"127 assert c.get("/forbidden-registered").data == b"forbidden-registered"Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 128–130why, one step back128129130def test_error_handler_blueprint(app):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 131why, one step back131 bp = flask.Blueprint("bp", __name__)Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 132–134why, one step back132133 @bp.errorhandler(500)134 def bp_exception_handler(e):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 135why, one step back135 return "bp-error"Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 136why, one step back136Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 137why, one step back137 @bp.route("/error")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 138–142why, one step back138 def bp_test():139 raise InternalServerError()140141 @app.errorhandler(500)142 def app_exception_handler(e):Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 143why, one step back143 return "app-error"Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 144why, one step back144Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 145why, one step back145 @app.route("/error")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 146–148why, one step back146 def app_test():147 raise InternalServerError()148Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 149why, one step back149 app.register_blueprint(bp, url_prefix="/bp")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 150–152why, one step back150151 c = app.test_client()152Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 153–154why, one step back153 assert c.get("/error").data == b"app-error"154 assert c.get("/bp/error").data == b"bp-error"Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 155–157why, one step back155156157def test_default_error_handler():Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 158why, one step back158 bp = flask.Blueprint("bp", __name__)Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 159–163why, one step back159160 @bp.errorhandler(HTTPException)161 def bp_exception_handler(e):162 assert isinstance(e, HTTPException)163 assert isinstance(e, NotFound)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 164why, one step back164 return "bp-default"Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 165–166why, one step back165166 @bp.errorhandler(Forbidden)Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86
line 167why, one step back167 def bp_forbidden_handler(e):Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 168why, one step back168 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 169why, one step back169 return "bp-forbidden"Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 170why, one step back170Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 171why, one step back171 @bp.route("/undefined")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 172–174why, one step back172 def bp_registered_test():173 raise NotFound()174Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 175why, one step back175 @bp.route("/forbidden")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 176–181why, one step back176 def bp_forbidden_test():177 raise Forbidden()178179 app = flask.Flask(__name__)180181 @app.errorhandler(HTTPException)Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86
line 182why, one step back182 def catchall_exception_handler(e):Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 183–184why, one step back183 assert isinstance(e, HTTPException)184 assert isinstance(e, NotFound)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 185why, one step back185 return "default"Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 186–187why, one step back186187 @app.errorhandler(Forbidden)Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86
line 188why, one step back188 def catchall_forbidden_handler(e):Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 189why, one step back189 assert isinstance(e, Forbidden)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 190why, one step back190 return "forbidden"Fix typo in issue templateGrey Libefore adoptiond7eea1b
line 191why, one step back191Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 192why, one step back192 @app.route("/forbidden")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 193–195why, one step back193 def forbidden():194 raise Forbidden()195Merge remote-tracking branch 'origin/1.0-maintenance'David Lordbefore adoptiona74864e
lines 196–199why, one step back196 @app.route("/slash/")197 def slash():198 return "slash"199Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 200why, one step back200 app.register_blueprint(bp, url_prefix="/bp")Fix typo in issue templateGrey Libefore adoptiond7eea1b
lines 201–202why, one step back201202 c = app.test_client()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 203–206why, one step back203 assert c.get("/bp/undefined").data == b"bp-default"204 assert c.get("/bp/forbidden").data == b"bp-forbidden"205 assert c.get("/undefined").data == b"default"206 assert c.get("/forbidden").data == b"forbidden"Merge remote-tracking branch 'origin/1.0-maintenance'David Lordbefore adoptiona74864e
lines 207–208why, one step back207 # Don't handle RequestRedirect raised when adding slash.208 assert c.get("/slash", follow_redirects=True).data == b"slash"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 209–210why, one step back209210Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 211why, one step back211class TestGenericHandlers:Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 212–241why, one step back212 """Test how very generic handlers are dispatched to."""213214 class Custom(Exception):215 pass216217 @pytest.fixture()218 def app(self, app):219 @app.route("/custom")220 def do_custom():221 raise self.Custom()222223 @app.route("/error")224 def do_error():225 raise KeyError()226227 @app.route("/abort")228 def do_abort():229 flask.abort(500)230231 @app.route("/raise")232 def do_raise():233 raise InternalServerError()234235 app.config["PROPAGATE_EXCEPTIONS"] = False236 return app237238 def report_error(self, e):239 original = getattr(e, "original_exception", None)240241 if original is not None:Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 242why, one step back242 return f"wrapped {type(original).__name__}"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
line 243why, one step back243Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374
line 244why, one step back244 return f"direct {type(e).__name__}"Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 245–289why, one step back245246 @pytest.mark.parametrize("to_handle", (InternalServerError, 500))247 def test_handle_class_or_code(self, app, client, to_handle):248 """``InternalServerError`` and ``500`` are aliases, they should249 have the same behavior. Both should only receive250 ``InternalServerError``, which might wrap another error.251 """252253 @app.errorhandler(to_handle)254 def handle_500(e):255 assert isinstance(e, InternalServerError)256 return self.report_error(e)257258 assert client.get("/custom").data == b"wrapped Custom"259 assert client.get("/error").data == b"wrapped KeyError"260 assert client.get("/abort").data == b"direct InternalServerError"261 assert client.get("/raise").data == b"direct InternalServerError"262263 def test_handle_generic_http(self, app, client):264 """``HTTPException`` should only receive ``HTTPException``265 subclasses. It will receive ``404`` routing exceptions.266 """267268 @app.errorhandler(HTTPException)269 def handle_http(e):270 assert isinstance(e, HTTPException)271 return str(e.code)272273 assert client.get("/error").data == b"500"274 assert client.get("/abort").data == b"500"275 assert client.get("/not-found").data == b"404"276277 def test_handle_generic(self, app, client):278 """Generic ``Exception`` will handle all exceptions directly,279 including ``HTTPExceptions``.280 """281282 @app.errorhandler(Exception)283 def handle_exception(e):284 return self.report_error(e)285286 assert client.get("/custom").data == b"direct Custom"287 assert client.get("/error").data == b"direct KeyError"288 assert client.get("/abort").data == b"direct InternalServerError"289 assert client.get("/not-found").data == b"direct NotFound"