MCPcopy Create free account
hub / github.com/pallets/flask / test_default_error_handler

Function test_default_error_handler

tests/test_user_error_handler.py:163–214  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

161
162
163def test_default_error_handler():
164 bp = flask.Blueprint("bp", __name__)
165
166 @bp.errorhandler(HTTPException)
167 def bp_exception_handler(e):
168 assert isinstance(e, HTTPException)
169 assert isinstance(e, NotFound)
170 return "bp-default"
171
172 @bp.errorhandler(Forbidden)
173 def bp_forbidden_handler(e):
174 assert isinstance(e, Forbidden)
175 return "bp-forbidden"
176
177 @bp.route("/undefined")
178 def bp_registered_test():
179 raise NotFound()
180
181 @bp.route("/forbidden")
182 def bp_forbidden_test():
183 raise Forbidden()
184
185 app = flask.Flask(__name__)
186
187 @app.errorhandler(HTTPException)
188 def catchall_exception_handler(e):
189 assert isinstance(e, HTTPException)
190 assert isinstance(e, NotFound)
191 return "default"
192
193 @app.errorhandler(Forbidden)
194 def catchall_forbidden_handler(e):
195 assert isinstance(e, Forbidden)
196 return "forbidden"
197
198 @app.route("/forbidden")
199 def forbidden():
200 raise Forbidden()
201
202 @app.route("/slash/")
203 def slash():
204 return "slash"
205
206 app.register_blueprint(bp, url_prefix="/bp")
207
208 c = app.test_client()
209 assert c.get("/bp/undefined").data == b"bp-default"
210 assert c.get("/bp/forbidden").data == b"bp-forbidden"
211 assert c.get("/undefined").data == b"default"
212 assert c.get("/forbidden").data == b"forbidden"
213 # Don't handle RequestRedirect raised when adding slash.
214 assert c.get("/slash", follow_redirects=True).data == b"slash"
215
216
217class TestGenericHandlers:

Callers

nothing calls this directly

Calls 3

test_clientMethod · 0.95
register_blueprintMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected