Skip to content
Myr

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 file

Take 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 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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

lines 6–13why, one step back
6
7import flask8910def test_error_handler_no_match(app, client):11 class CustomException(Exception):12 pass13

Merge pull request #3574 from jackwardell/adding-error-message-to-assertDavid Lordbefore adoptiond091bb0

lines 14–16why, one step back
14 class UnacceptableCustomException(BaseException):
15 pass16

Fix typo in issue templateGrey Libefore adoptiond7eea1b

lines 17–19why, one step back
17 @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 back
20 return "custom"

Merge pull request #3574 from jackwardell/adding-error-message-to-assertDavid Lordbefore adoptiond091bb0

lines 21–25why, one step back
21
22 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 back
26
27 @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 back
29 assert isinstance(e, InternalServerError)
30 original = getattr(e, "original_exception", None)

Fix typo in issue templateGrey Libefore adoptiond7eea1b

line 31why, one step back
31

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

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

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

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

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

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

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

lines 51–52why, one step back
51 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 back
53
5455def 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 back
68 return "parent"

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

line 74why, one step back
74

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

lines 84–88why, one step back
84 def registered_test():
85 raise ChildExceptionRegistered()8687 c = app.test_client()88

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

lines 89–91why, one step back
89 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 back
92
9394def 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 back
104 return "forbidden"

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

line 110why, one step back
110

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

lines 120–124why, one step back
120 def unregistered_test():
121 raise ForbiddenSubclassUnregistered()122123 c = app.test_client()124

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

lines 125–127why, one step back
125 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 back
128
129130def test_error_handler_blueprint(app):

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

line 136why, one step back
136

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

line 144why, one step back
144

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

line 170why, one step back
170

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

line 191why, one step back
191

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

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

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

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

Fix typo in issue templateGrey Libefore adoptiond7eea1b

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

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

lines 203–206why, one step back
203 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 back
207 # 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 back
209
210

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

line 211why, one step back
211class TestGenericHandlers:

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

lines 212–241why, one step back
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:

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

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

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

line 243why, one step back
243

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

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

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

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