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

Function test_error_handler_no_match

tests/test_user_error_handler.py:10–58  ·  view source on GitHub ↗
(app, client)

Source from the content-addressed store, hash-verified

8
9
10def test_error_handler_no_match(app, client):
11 class CustomException(Exception):
12 pass
13
14 @app.errorhandler(CustomException)
15 def custom_exception_handler(e):
16 assert isinstance(e, CustomException)
17 return "custom"
18
19 with pytest.raises(TypeError) as exc_info:
20 app.register_error_handler(CustomException(), None)
21
22 assert "CustomException() is an instance, not a class." in str(exc_info.value)
23
24 with pytest.raises(ValueError) as exc_info:
25 app.register_error_handler(list, None)
26
27 assert "'list' is not a subclass of Exception." in str(exc_info.value)
28
29 @app.errorhandler(500)
30 def handle_500(e):
31 assert isinstance(e, InternalServerError)
32
33 if e.original_exception is not None:
34 return f"wrapped {type(e.original_exception).__name__}"
35
36 return "direct"
37
38 with pytest.raises(ValueError) as exc_info:
39 app.register_error_handler(999, None)
40
41 assert "Use a subclass of HTTPException" in str(exc_info.value)
42
43 @app.route("/custom")
44 def custom_test():
45 raise CustomException()
46
47 @app.route("/keyerror")
48 def key_error():
49 raise KeyError()
50
51 @app.route("/abort")
52 def do_abort():
53 flask.abort(500)
54
55 app.testing = False
56 assert client.get("/custom").data == b"custom"
57 assert client.get("/keyerror").data == b"wrapped KeyError"
58 assert client.get("/abort").data == b"direct"
59
60
61def test_error_handler_subclass(app):

Callers

nothing calls this directly

Calls 3

CustomExceptionClass · 0.85
getMethod · 0.45

Tested by

no test coverage detected