Skip to content
Myr

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 file

Take 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 back
1# -*- coding: utf-8 -*-
2"""3tests.test_user_error_handler4~~~~~~~~~~~~~~~~~~~~~~~~~~~~~5

Merge branch '1.0.x'David Lordbefore adoption1351d0a

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

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

line 8why, one step back
8"""

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

line 9why, one step back
9import pytest

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

lines 10–13why, one step back
10from werkzeug.exceptions import Forbidden
11from werkzeug.exceptions import HTTPException12from werkzeug.exceptions import InternalServerError13from werkzeug.exceptions import NotFound

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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)

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

line 31why, one step back
31

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

lines 32–36why, one step back
32 if original is not None:
33 return "wrapped " + type(original).__name__3435 return "direct"36

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

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

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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__)

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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__)

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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):

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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):

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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):

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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"

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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")

add changelog for docs theme and tutorialDavid Lordbefore adoptionbe46acb

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–289why, one step back
209
210211class 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"