Skip to content
Myr

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 file

Take 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 back
1import pytest

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

lines 2–5why, one step back
2from werkzeug.exceptions import Forbidden
3from werkzeug.exceptions import HTTPException4from werkzeug.exceptions import InternalServerError5from werkzeug.exceptions import NotFound

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 6–16why, one step back
6
7import 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 back
17 return "custom"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 18–20why, one step back
18
19 @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 back
21 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 back
23

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

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

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

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

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

lines 26–28why, one step back
26
27 return "direct"28

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 30–32why, one step back
30 def custom_test():
31 raise CustomException()32

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 34–36why, one step back
34 def key_error():
35 raise KeyError()36

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

lines 37–40why, one step back
37 @app.route("/abort")
38 def do_abort():39 flask.abort(500)40

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

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

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

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

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

lines 43–44why, one step back
43 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 back
45
4647def 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 back
60 return "parent"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 61–64why, one step back
61
62 @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 back
65 return "child-registered"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 66why, one step back
66

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 68–70why, one step back
68 def parent_test():
69 raise ParentException()70

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 72–74why, one step back
72 def unregistered_test():
73 raise ChildExceptionUnregistered()74

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 76–80why, one step back
76 def registered_test():
77 raise ChildExceptionRegistered()7879 c = app.test_client()80

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

lines 81–83why, one step back
81 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 back
84
8586def 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 back
96 return "forbidden"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 97–100why, one step back
97
98 @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 back
101 return "forbidden-registered"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 102why, one step back
102

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 104–106why, one step back
104 def forbidden_test():
105 raise Forbidden()106

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 108–110why, one step back
108 def registered_test():
109 raise ForbiddenSubclassRegistered()110

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 112–116why, one step back
112 def unregistered_test():
113 raise ForbiddenSubclassUnregistered()114115 c = app.test_client()116

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

lines 117–119why, one step back
117 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 back
120
121122def test_error_handler_blueprint(app):

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 124–126why, one step back
124
125 @bp.errorhandler(500)126 def bp_exception_handler(e):

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 128why, one step back
128

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 130–134why, one step back
130 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 back
135 return "app-error"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 136why, one step back
136

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 138–140why, one step back
138 def app_test():
139 raise InternalServerError()140

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 142–144why, one step back
142
143 c = app.test_client()144

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

lines 145–146why, one step back
145 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 back
147
148149def test_default_error_handler():

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 151–155why, one step back
151
152 @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 back
156 return "bp-default"

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 157–158why, one step back
157
158 @bp.errorhandler(Forbidden)

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

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

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 162why, one step back
162

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 164–166why, one step back
164 def bp_registered_test():
165 raise NotFound()166

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 168–173why, one step back
168 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 back
174 def catchall_exception_handler(e):

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 175–176why, one step back
175 assert isinstance(e, HTTPException)
176 assert isinstance(e, NotFound)

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 178–179why, one step back
178
179 @app.errorhandler(Forbidden)

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

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

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

line 183why, one step back
183

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 185–187why, one step back
185 def forbidden():
186 raise Forbidden()187

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

lines 188–191why, one step back
188 @app.route("/slash/")
189 def slash():190 return "slash"191

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

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

Merge pull request #2707 from rochacbruno/lazy_loader_messageDavid Lordbefore adoptione102e5c

lines 193–194why, one step back
193
194 c = app.test_client()

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

lines 195–198why, one step back
195 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 back
199 # 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 back
201
202

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

line 203why, one step back
203class TestGenericHandlers:

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

lines 204–233why, one step back
204 """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 back
234 return f"wrapped {type(original).__name__}"

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

line 235why, one step back
235

Merge pull request #3554 from pallets/drop-python2David Lordbefore adoptioncd8a374

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

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

lines 237–281why, one step back
237
238 @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"