test_user_error_handler.py
This file
Path
tests/test_user_error_handler.py
289 lines · 7.9 KiB · Python
Last changed by
Merge branch '1.0.x'David Lordbefore adoption1351d0a
History of this fileTake it
The walk stopped after 200 commits: lines older than be46acb point at it.
add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 1–5why, one step back1# -*- coding: utf-8 -*-2"""3tests.test_user_error_handler4~~~~~~~~~~~~~~~~~~~~~~~~~~~~~5Merge branch '1.0.x'David Lordbefore adoption1351d0a
lines 6–7why, one step back6:copyright: 2010 Pallets7:license: BSD-3-Clauseadd changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
line 8why, one step back8"""Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
line 9why, one step back9import pytestMerge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
lines 10–13why, one step back10from werkzeug.exceptions import Forbidden11from werkzeug.exceptions import HTTPException12from werkzeug.exceptions import InternalServerError13from werkzeug.exceptions import NotFoundadd changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
lines 14–24why, one step back1415import flask161718def test_error_handler_no_match(app, client):19 class CustomException(Exception):20 pass2122 @app.errorhandler(CustomException)23 def custom_exception_handler(e):24 assert isinstance(e, CustomException)Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 25why, one step back25 return "custom"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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)add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
line 31why, one step back31Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb
lines 32–36why, one step back32 if original is not None:33 return "wrapped " + type(original).__name__3435 return "direct"36Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 37why, one step back37 @app.route("/custom")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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)48add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
line 74why, one step back74Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 75why, one step back75 @app.route("/parent")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
line 110why, one step back110Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 111why, one step back111 @app.route("/forbidden")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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__)add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
line 136why, one step back136Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 137why, one step back137 @bp.route("/error")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
line 144why, one step back144Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 145why, one step back145 @app.route("/error")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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__)add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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):add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
line 170why, one step back170Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 171why, one step back171 @bp.route("/undefined")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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):add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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):add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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"add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
line 191why, one step back191Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 192why, one step back192 @app.route("/forbidden")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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")add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb
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–289why, one step back209210211class TestGenericHandlers(object):212 """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:242 return "wrapped " + type(original).__name__243244 return "direct " + type(e).__name__245246 @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"