Skip to content
Myr

test_user_error_handler.py

This file

Path

tests/test_user_error_handler.py

193 lines · 4.9 KiB · Python

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 49f3e96 point at it.

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 1why, one step back
1# -*- coding: utf-8 -*-

Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c

lines 2–9why, one step back
2"""
3tests.test_user_error_handler4~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56:copyright: © 2010 by the Pallets team.7:license: BSD, see LICENSE for more details.8"""9

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

line 10why, one step back
10from werkzeug.exceptions import Forbidden, InternalServerError, HTTPException, NotFound

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 11–20why, one step back
11import flask
121314def test_error_handler_no_match(app, client):15 class CustomException(Exception):16 pass1718 @app.errorhandler(CustomException)19 def custom_exception_handler(e):20 assert isinstance(e, CustomException)

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

line 21why, one step back
21 return "custom"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 22–26why, one step back
22
23 @app.errorhandler(500)24 def handle_500(e):25 return type(e).__name__26

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 28–30why, one step back
28 def custom_test():
29 raise CustomException()30

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 32–35why, one step back
32 def key_error():
33 raise KeyError()3435 app.testing = False

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

lines 36–37why, one step back
36 assert client.get("/custom").data == b"custom"
37 assert client.get("/keyerror").data == b"KeyError"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 38–52why, one step back
38
3940def test_error_handler_subclass(app):41 class ParentException(Exception):42 pass4344 class ChildExceptionUnregistered(ParentException):45 pass4647 class ChildExceptionRegistered(ParentException):48 pass4950 @app.errorhandler(ParentException)51 def parent_exception_handler(e):52 assert isinstance(e, ParentException)

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

line 53why, one step back
53 return "parent"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 54–57why, one step back
54
55 @app.errorhandler(ChildExceptionRegistered)56 def child_exception_handler(e):57 assert isinstance(e, ChildExceptionRegistered)

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

line 58why, one step back
58 return "child-registered"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 59why, one step back
59

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 61–63why, one step back
61 def parent_test():
62 raise ParentException()63

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 65–67why, one step back
65 def unregistered_test():
66 raise ChildExceptionUnregistered()67

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 69–73why, one step back
69 def registered_test():
70 raise ChildExceptionRegistered()7172 c = app.test_client()73

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

lines 74–76why, one step back
74 assert c.get("/parent").data == b"parent"
75 assert c.get("/child-unregistered").data == b"parent"76 assert c.get("/child-registered").data == b"child-registered"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 77–88why, one step back
77
7879def test_error_handler_http_subclass(app):80 class ForbiddenSubclassRegistered(Forbidden):81 pass8283 class ForbiddenSubclassUnregistered(Forbidden):84 pass8586 @app.errorhandler(403)87 def code_exception_handler(e):88 assert isinstance(e, Forbidden)

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 90–93why, one step back
90
91 @app.errorhandler(ForbiddenSubclassRegistered)92 def subclass_exception_handler(e):93 assert isinstance(e, ForbiddenSubclassRegistered)

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

line 94why, one step back
94 return "forbidden-registered"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 95why, one step back
95

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 97–99why, one step back
97 def forbidden_test():
98 raise Forbidden()99

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 101–103why, one step back
101 def registered_test():
102 raise ForbiddenSubclassRegistered()103

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 105–109why, one step back
105 def unregistered_test():
106 raise ForbiddenSubclassUnregistered()107108 c = app.test_client()109

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

lines 110–112why, one step back
110 assert c.get("/forbidden").data == b"forbidden"
111 assert c.get("/forbidden-unregistered").data == b"forbidden"112 assert c.get("/forbidden-registered").data == b"forbidden-registered"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 113–115why, one step back
113
114115def test_error_handler_blueprint(app):

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 117–119why, one step back
117
118 @bp.errorhandler(500)119 def bp_exception_handler(e):

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 121why, one step back
121

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 123–127why, one step back
123 def bp_test():
124 raise InternalServerError()125126 @app.errorhandler(500)127 def app_exception_handler(e):

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

line 128why, one step back
128 return "app-error"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 129why, one step back
129

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 131–133why, one step back
131 def app_test():
132 raise InternalServerError()133

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 135–137why, one step back
135
136 c = app.test_client()137

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

lines 138–139why, one step back
138 assert c.get("/error").data == b"app-error"
139 assert c.get("/bp/error").data == b"bp-error"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 140–142why, one step back
140
141142def test_default_error_handler():

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 144–148why, one step back
144
145 @bp.errorhandler(HTTPException)146 def bp_exception_handler(e):147 assert isinstance(e, HTTPException)148 assert isinstance(e, NotFound)

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

line 149why, one step back
149 return "bp-default"

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 150–153why, one step back
150
151 @bp.errorhandler(Forbidden)152 def bp_exception_handler(e):153 assert isinstance(e, Forbidden)

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 155why, one step back
155

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 157–159why, one step back
157 def bp_registered_test():
158 raise NotFound()159

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 161–169why, one step back
161 def bp_forbidden_test():
162 raise Forbidden()163164 app = flask.Flask(__name__)165166 @app.errorhandler(HTTPException)167 def catchall_errorhandler(e):168 assert isinstance(e, HTTPException)169 assert isinstance(e, NotFound)

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 171–174why, one step back
171
172 @app.errorhandler(Forbidden)173 def catchall_errorhandler(e):174 assert isinstance(e, Forbidden)

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

line 176why, one step back
176

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 178–180why, one step back
178 def forbidden():
179 raise Forbidden()180

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

lines 181–184why, one step back
181 @app.route("/slash/")
182 def slash():183 return "slash"184

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

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

Merge pull request #2353 from grantwwu/patch-1David Lordbefore adoption49f3e96

lines 186–187why, one step back
186
187 c = app.test_client()

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

lines 188–191why, one step back
188 assert c.get("/bp/undefined").data == b"bp-default"
189 assert c.get("/bp/forbidden").data == b"bp-forbidden"190 assert c.get("/undefined").data == b"default"191 assert c.get("/forbidden").data == b"forbidden"

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

lines 192–193why, one step back
192 # Don't handle RequestRedirect raised when adding slash.
193 assert c.get("/slash", follow_redirects=True).data == b"slash"