| 
									
										
										
										
											2019-06-20 04:58:55 +08:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2019-06-01 23:35:03 +08:00
										 |  |  | from werkzeug.exceptions import Forbidden | 
					
						
							|  |  |  | from werkzeug.exceptions import HTTPException | 
					
						
							|  |  |  | from werkzeug.exceptions import InternalServerError | 
					
						
							|  |  |  | from werkzeug.exceptions import NotFound | 
					
						
							| 
									
										
										
										
											2018-02-09 02:57:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | import flask | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handler_no_match(app, client): | 
					
						
							| 
									
										
										
										
											2015-06-03 04:13:30 +08:00
										 |  |  |     class CustomException(Exception): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(CustomException) | 
					
						
							|  |  |  |     def custom_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, CustomException) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "custom" | 
					
						
							| 
									
										
										
										
											2015-06-03 04:13:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-29 01:49:23 +08:00
										 |  |  |     with pytest.raises(TypeError) as exc_info: | 
					
						
							|  |  |  |         app.register_error_handler(CustomException(), None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert "CustomException() is an instance, not a class." in str(exc_info.value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with pytest.raises(ValueError) as exc_info: | 
					
						
							|  |  |  |         app.register_error_handler(list, None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert "'list' is not a subclass of Exception." in str(exc_info.value) | 
					
						
							| 
									
										
										
										
											2020-04-17 00:01:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-03 04:13:30 +08:00
										 |  |  |     @app.errorhandler(500) | 
					
						
							|  |  |  |     def handle_500(e): | 
					
						
							| 
									
										
										
										
											2019-06-20 04:58:55 +08:00
										 |  |  |         assert isinstance(e, InternalServerError) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-29 01:49:23 +08:00
										 |  |  |         if e.original_exception is not None: | 
					
						
							|  |  |  |             return f"wrapped {type(e.original_exception).__name__}" | 
					
						
							| 
									
										
										
										
											2019-06-20 04:58:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return "direct" | 
					
						
							| 
									
										
										
										
											2015-06-03 04:13:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-29 01:49:23 +08:00
										 |  |  |     with pytest.raises(ValueError) as exc_info: | 
					
						
							|  |  |  |         app.register_error_handler(999, None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert "Use a subclass of HTTPException" in str(exc_info.value) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/custom") | 
					
						
							| 
									
										
										
										
											2015-06-03 04:13:30 +08:00
										 |  |  |     def custom_test(): | 
					
						
							|  |  |  |         raise CustomException() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/keyerror") | 
					
						
							| 
									
										
										
										
											2015-06-03 04:13:30 +08:00
										 |  |  |     def key_error(): | 
					
						
							|  |  |  |         raise KeyError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 04:58:55 +08:00
										 |  |  |     @app.route("/abort") | 
					
						
							|  |  |  |     def do_abort(): | 
					
						
							|  |  |  |         flask.abort(500) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  |     app.testing = False | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert client.get("/custom").data == b"custom" | 
					
						
							| 
									
										
										
										
											2019-06-20 04:58:55 +08:00
										 |  |  |     assert client.get("/keyerror").data == b"wrapped KeyError" | 
					
						
							|  |  |  |     assert client.get("/abort").data == b"direct" | 
					
						
							| 
									
										
										
										
											2015-06-03 04:13:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handler_subclass(app): | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ParentException(Exception): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ChildExceptionUnregistered(ParentException): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ChildExceptionRegistered(ParentException): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.errorhandler(ParentException) | 
					
						
							|  |  |  |     def parent_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, ParentException) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "parent" | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.errorhandler(ChildExceptionRegistered) | 
					
						
							|  |  |  |     def child_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, ChildExceptionRegistered) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "child-registered" | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/parent") | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     def parent_test(): | 
					
						
							|  |  |  |         raise ParentException() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/child-unregistered") | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     def unregistered_test(): | 
					
						
							|  |  |  |         raise ChildExceptionUnregistered() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/child-registered") | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     def registered_test(): | 
					
						
							|  |  |  |         raise ChildExceptionRegistered() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     c = app.test_client() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert c.get("/parent").data == b"parent" | 
					
						
							|  |  |  |     assert c.get("/child-unregistered").data == b"parent" | 
					
						
							|  |  |  |     assert c.get("/child-registered").data == b"child-registered" | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handler_http_subclass(app): | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ForbiddenSubclassRegistered(Forbidden): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     class ForbiddenSubclassUnregistered(Forbidden): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.errorhandler(403) | 
					
						
							|  |  |  |     def code_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, Forbidden) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "forbidden" | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     @app.errorhandler(ForbiddenSubclassRegistered) | 
					
						
							|  |  |  |     def subclass_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, ForbiddenSubclassRegistered) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "forbidden-registered" | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/forbidden") | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     def forbidden_test(): | 
					
						
							|  |  |  |         raise Forbidden() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/forbidden-registered") | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     def registered_test(): | 
					
						
							|  |  |  |         raise ForbiddenSubclassRegistered() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/forbidden-unregistered") | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     def unregistered_test(): | 
					
						
							|  |  |  |         raise ForbiddenSubclassUnregistered() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     c = app.test_client() | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert c.get("/forbidden").data == b"forbidden" | 
					
						
							|  |  |  |     assert c.get("/forbidden-unregistered").data == b"forbidden" | 
					
						
							|  |  |  |     assert c.get("/forbidden-registered").data == b"forbidden-registered" | 
					
						
							| 
									
										
										
										
											2014-12-29 00:19:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 06:18:39 +08:00
										 |  |  | def test_error_handler_blueprint(app): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     bp = flask.Blueprint("bp", __name__) | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @bp.errorhandler(500) | 
					
						
							|  |  |  |     def bp_exception_handler(e): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "bp-error" | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @bp.route("/error") | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     def bp_test(): | 
					
						
							|  |  |  |         raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(500) | 
					
						
							|  |  |  |     def app_exception_handler(e): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "app-error" | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/error") | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  |     def app_test(): | 
					
						
							|  |  |  |         raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.register_blueprint(bp, url_prefix="/bp") | 
					
						
							| 
									
										
										
										
											2015-04-12 02:42:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     c = app.test_client() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert c.get("/error").data == b"app-error" | 
					
						
							|  |  |  |     assert c.get("/bp/error").data == b"bp-error" | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_default_error_handler(): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     bp = flask.Blueprint("bp", __name__) | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @bp.errorhandler(HTTPException) | 
					
						
							|  |  |  |     def bp_exception_handler(e): | 
					
						
							|  |  |  |         assert isinstance(e, HTTPException) | 
					
						
							|  |  |  |         assert isinstance(e, NotFound) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "bp-default" | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @bp.errorhandler(Forbidden) | 
					
						
							| 
									
										
										
										
											2019-06-01 02:53:26 +08:00
										 |  |  |     def bp_forbidden_handler(e): | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  |         assert isinstance(e, Forbidden) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "bp-forbidden" | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @bp.route("/undefined") | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  |     def bp_registered_test(): | 
					
						
							|  |  |  |         raise NotFound() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @bp.route("/forbidden") | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  |     def bp_forbidden_test(): | 
					
						
							|  |  |  |         raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     app = flask.Flask(__name__) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(HTTPException) | 
					
						
							| 
									
										
										
										
											2019-06-01 02:53:26 +08:00
										 |  |  |     def catchall_exception_handler(e): | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  |         assert isinstance(e, HTTPException) | 
					
						
							|  |  |  |         assert isinstance(e, NotFound) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "default" | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @app.errorhandler(Forbidden) | 
					
						
							| 
									
										
										
										
											2019-06-01 02:53:26 +08:00
										 |  |  |     def catchall_forbidden_handler(e): | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  |         assert isinstance(e, Forbidden) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         return "forbidden" | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     @app.route("/forbidden") | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  |     def forbidden(): | 
					
						
							|  |  |  |         raise Forbidden() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-08 01:52:54 +08:00
										 |  |  |     @app.route("/slash/") | 
					
						
							| 
									
										
										
										
											2018-11-02 04:41:53 +08:00
										 |  |  |     def slash(): | 
					
						
							| 
									
										
										
										
											2019-01-08 01:52:54 +08:00
										 |  |  |         return "slash" | 
					
						
							| 
									
										
										
										
											2018-11-02 04:41:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     app.register_blueprint(bp, url_prefix="/bp") | 
					
						
							| 
									
										
										
										
											2017-05-23 06:57:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     c = app.test_client() | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     assert c.get("/bp/undefined").data == b"bp-default" | 
					
						
							|  |  |  |     assert c.get("/bp/forbidden").data == b"bp-forbidden" | 
					
						
							|  |  |  |     assert c.get("/undefined").data == b"default" | 
					
						
							|  |  |  |     assert c.get("/forbidden").data == b"forbidden" | 
					
						
							| 
									
										
										
										
											2019-01-08 01:52:54 +08:00
										 |  |  |     # Don't handle RequestRedirect raised when adding slash. | 
					
						
							|  |  |  |     assert c.get("/slash", follow_redirects=True).data == b"slash" | 
					
						
							| 
									
										
										
										
											2019-06-20 04:58:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 00:43:06 +08:00
										 |  |  | class TestGenericHandlers: | 
					
						
							| 
									
										
										
										
											2019-06-20 04:58:55 +08:00
										 |  |  |     """Test how very generic handlers are dispatched to.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class Custom(Exception): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @pytest.fixture() | 
					
						
							|  |  |  |     def app(self, app): | 
					
						
							|  |  |  |         @app.route("/custom") | 
					
						
							|  |  |  |         def do_custom(): | 
					
						
							|  |  |  |             raise self.Custom() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.route("/error") | 
					
						
							|  |  |  |         def do_error(): | 
					
						
							|  |  |  |             raise KeyError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.route("/abort") | 
					
						
							|  |  |  |         def do_abort(): | 
					
						
							|  |  |  |             flask.abort(500) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.route("/raise") | 
					
						
							|  |  |  |         def do_raise(): | 
					
						
							|  |  |  |             raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         app.config["PROPAGATE_EXCEPTIONS"] = False | 
					
						
							|  |  |  |         return app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def report_error(self, e): | 
					
						
							|  |  |  |         original = getattr(e, "original_exception", None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if original is not None: | 
					
						
							| 
									
										
										
										
											2020-04-05 02:39:03 +08:00
										 |  |  |             return f"wrapped {type(original).__name__}" | 
					
						
							| 
									
										
										
										
											2019-06-20 04:58:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-05 02:39:03 +08:00
										 |  |  |         return f"direct {type(e).__name__}" | 
					
						
							| 
									
										
										
										
											2019-06-20 04:58:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @pytest.mark.parametrize("to_handle", (InternalServerError, 500)) | 
					
						
							|  |  |  |     def test_handle_class_or_code(self, app, client, to_handle): | 
					
						
							|  |  |  |         """``InternalServerError`` and ``500`` are aliases, they should
 | 
					
						
							|  |  |  |         have the same behavior. Both should only receive | 
					
						
							|  |  |  |         ``InternalServerError``, which might wrap another error. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.errorhandler(to_handle) | 
					
						
							|  |  |  |         def handle_500(e): | 
					
						
							|  |  |  |             assert isinstance(e, InternalServerError) | 
					
						
							|  |  |  |             return self.report_error(e) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert client.get("/custom").data == b"wrapped Custom" | 
					
						
							|  |  |  |         assert client.get("/error").data == b"wrapped KeyError" | 
					
						
							|  |  |  |         assert client.get("/abort").data == b"direct InternalServerError" | 
					
						
							|  |  |  |         assert client.get("/raise").data == b"direct InternalServerError" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_handle_generic_http(self, app, client): | 
					
						
							|  |  |  |         """``HTTPException`` should only receive ``HTTPException``
 | 
					
						
							|  |  |  |         subclasses. It will receive ``404`` routing exceptions. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.errorhandler(HTTPException) | 
					
						
							|  |  |  |         def handle_http(e): | 
					
						
							|  |  |  |             assert isinstance(e, HTTPException) | 
					
						
							|  |  |  |             return str(e.code) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert client.get("/error").data == b"500" | 
					
						
							|  |  |  |         assert client.get("/abort").data == b"500" | 
					
						
							|  |  |  |         assert client.get("/not-found").data == b"404" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_handle_generic(self, app, client): | 
					
						
							|  |  |  |         """Generic ``Exception`` will handle all exceptions directly,
 | 
					
						
							|  |  |  |         including ``HTTPExceptions``. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @app.errorhandler(Exception) | 
					
						
							|  |  |  |         def handle_exception(e): | 
					
						
							|  |  |  |             return self.report_error(e) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert client.get("/custom").data == b"direct Custom" | 
					
						
							|  |  |  |         assert client.get("/error").data == b"direct KeyError" | 
					
						
							|  |  |  |         assert client.get("/abort").data == b"direct InternalServerError" | 
					
						
							|  |  |  |         assert client.get("/not-found").data == b"direct NotFound" |