diff --git a/src/flask/app.py b/src/flask/app.py index 1143f20d..3b9d062d 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -1270,7 +1270,9 @@ class Flask(_PackageBoundObject): else: exc_class = exc_class_or_code - assert issubclass(exc_class, Exception) + assert issubclass( + exc_class, Exception + ), "Custom exceptions must be subclasses of Exception." if issubclass(exc_class, HTTPException): return exc_class, exc_class.code diff --git a/tests/test_user_error_handler.py b/tests/test_user_error_handler.py index cd2b5864..d9f94a3f 100644 --- a/tests/test_user_error_handler.py +++ b/tests/test_user_error_handler.py @@ -11,11 +11,19 @@ def test_error_handler_no_match(app, client): class CustomException(Exception): pass + class UnacceptableCustomException(BaseException): + pass + @app.errorhandler(CustomException) def custom_exception_handler(e): assert isinstance(e, CustomException) return "custom" + with pytest.raises( + AssertionError, match="Custom exceptions must be subclasses of Exception." + ): + app.register_error_handler(UnacceptableCustomException, None) + @app.errorhandler(500) def handle_500(e): assert isinstance(e, InternalServerError)