Skip to content
Myr

test_user_error_handler.py

This file

Path

tests/test_user_error_handler.py

288 lines · 7.9 KiB · Python

Last changed by

f-strings everywhereDavid Lordbefore adoption2ae740d

History of this file

Take 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 back
1"""
2tests.test_user_error_handler3~~~~~~~~~~~~~~~~~~~~~~~~~~~~~4

Merge branch '1.0.x'David Lordbefore adoption1351d0a

lines 5–6why, one step back
5:copyright: 2010 Pallets
6:license: BSD-3-Clause

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 7why, one step back
7"""

Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb

line 8why, one step back
8import pytest

Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697

lines 9–12why, one step back
9from werkzeug.exceptions import Forbidden
10from werkzeug.exceptions import HTTPException11from werkzeug.exceptions import InternalServerError12from werkzeug.exceptions import NotFound

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 13–23why, one step back
13
14import 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 back
24 return "custom"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 25–27why, one step back
25
26 @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 back
28 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 back
30

Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb

line 31why, one step back
31 if original is not None:

f-strings everywhereDavid Lordbefore adoption2ae740d

line 32why, one step back
32 return f"wrapped {type(original).__name__}"

Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb

lines 33–35why, one step back
33
34 return "direct"35

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 36why, one step back
36 @app.route("/custom")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 37–39why, one step back
37 def custom_test():
38 raise CustomException()39

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 40why, one step back
40 @app.route("/keyerror")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 41–43why, one step back
41 def key_error():
42 raise KeyError()43

Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb

lines 44–47why, one step back
44 @app.route("/abort")
45 def do_abort():46 flask.abort(500)47

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 48why, one step back
48 app.testing = False

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 49why, one step back
49 assert client.get("/custom").data == b"custom"

Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb

lines 50–51why, one step back
50 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 back
52
5354def 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 back
67 return "parent"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 68–71why, one step back
68
69 @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 back
72 return "child-registered"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 73why, one step back
73

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 74why, one step back
74 @app.route("/parent")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 75–77why, one step back
75 def parent_test():
76 raise ParentException()77

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 78why, one step back
78 @app.route("/child-unregistered")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 79–81why, one step back
79 def unregistered_test():
80 raise ChildExceptionUnregistered()81

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 82why, one step back
82 @app.route("/child-registered")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 83–87why, one step back
83 def registered_test():
84 raise ChildExceptionRegistered()8586 c = app.test_client()87

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 88–90why, one step back
88 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 back
91
9293def 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 back
103 return "forbidden"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 104–107why, one step back
104
105 @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 back
108 return "forbidden-registered"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 109why, one step back
109

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 110why, one step back
110 @app.route("/forbidden")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 111–113why, one step back
111 def forbidden_test():
112 raise Forbidden()113

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 114why, one step back
114 @app.route("/forbidden-registered")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 115–117why, one step back
115 def registered_test():
116 raise ForbiddenSubclassRegistered()117

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 118why, one step back
118 @app.route("/forbidden-unregistered")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 119–123why, one step back
119 def unregistered_test():
120 raise ForbiddenSubclassUnregistered()121122 c = app.test_client()123

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 124–126why, one step back
124 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 back
127
128129def test_error_handler_blueprint(app):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 130why, one step back
130 bp = flask.Blueprint("bp", __name__)

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 131–133why, one step back
131
132 @bp.errorhandler(500)133 def bp_exception_handler(e):

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 134why, one step back
134 return "bp-error"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 135why, one step back
135

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 136why, one step back
136 @bp.route("/error")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 137–141why, one step back
137 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 back
142 return "app-error"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 143why, one step back
143

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 144why, one step back
144 @app.route("/error")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 145–147why, one step back
145 def app_test():
146 raise InternalServerError()147

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 148why, one step back
148 app.register_blueprint(bp, url_prefix="/bp")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 149–151why, one step back
149
150 c = app.test_client()151

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 152–153why, one step back
152 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 back
154
155156def test_default_error_handler():

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 157why, one step back
157 bp = flask.Blueprint("bp", __name__)

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 158–162why, one step back
158
159 @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 back
163 return "bp-default"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 164–165why, one step back
164
165 @bp.errorhandler(Forbidden)

Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86

line 166why, one step back
166 def bp_forbidden_handler(e):

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 167why, one step back
167 assert isinstance(e, Forbidden)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 168why, one step back
168 return "bp-forbidden"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 169why, one step back
169

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 170why, one step back
170 @bp.route("/undefined")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 171–173why, one step back
171 def bp_registered_test():
172 raise NotFound()173

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 174why, one step back
174 @bp.route("/forbidden")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 175–180why, one step back
175 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 back
181 def catchall_exception_handler(e):

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 182–183why, one step back
182 assert isinstance(e, HTTPException)
183 assert isinstance(e, NotFound)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 184why, one step back
184 return "default"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 185–186why, one step back
185
186 @app.errorhandler(Forbidden)

Merge pull request #3241 from jon-stumpf/jss-updatesDavid Lordbefore adoption48d0e86

line 187why, one step back
187 def catchall_forbidden_handler(e):

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 188why, one step back
188 assert isinstance(e, Forbidden)

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 189why, one step back
189 return "forbidden"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 190why, one step back
190

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 191why, one step back
191 @app.route("/forbidden")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 192–194why, one step back
192 def forbidden():
193 raise Forbidden()194

Merge remote-tracking branch 'origin/1.0-maintenance'David Lordbefore adoptiona74864e

lines 195–198why, one step back
195 @app.route("/slash/")
196 def slash():197 return "slash"198

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

line 199why, one step back
199 app.register_blueprint(bp, url_prefix="/bp")

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 200–201why, one step back
200
201 c = app.test_client()

Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032

lines 202–205why, one step back
202 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 back
206 # 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 back
208
209

apply pyupgradeDavid Lordbefore adoption524fd0b

line 210why, one step back
210class TestGenericHandlers:

Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb

lines 211–240why, one step back
211 """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:

f-strings everywhereDavid Lordbefore adoption2ae740d

line 241why, one step back
241 return f"wrapped {type(original).__name__}"

Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb

line 242why, one step back
242

f-strings everywhereDavid Lordbefore adoption2ae740d

line 243why, one step back
243 return f"direct {type(e).__name__}"

Merge pull request #3266 from pallets/internal-server-error-typeDavid Lordbefore adoption185d7cb

lines 244–288why, one step back
244
245 @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"