From 7bf508f13b59bd2681d5087f3f581038cd5b7966 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Tue, 18 Apr 2023 15:10:38 +0900 Subject: [PATCH] Improve the default handler when raise_error_for_unhandled_request is true (#885) --- slack_bolt/app/app.py | 30 ++++++++++++++++----------- slack_bolt/app/async_app.py | 30 ++++++++++++++++----------- slack_bolt/error/__init__.py | 3 +++ tests/slack_bolt/error/__init__.py | 0 tests/slack_bolt/error/test_errors.py | 19 +++++++++++++++++ 5 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 tests/slack_bolt/error/__init__.py create mode 100644 tests/slack_bolt/error/test_errors.py diff --git a/slack_bolt/app/app.py b/slack_bolt/app/app.py index e79cac07c..d01163636 100644 --- a/slack_bolt/app/app.py +++ b/slack_bolt/app/app.py @@ -511,15 +511,18 @@ def middleware_next(): # This should not be an intentional handling in usual use cases. resp = BoltResponse(status=404, body={"error": "no next() calls in middleware"}) if self._raise_error_for_unhandled_request is True: - self._listener_runner.listener_error_handler.handle( - error=BoltUnhandledRequestError( + try: + raise BoltUnhandledRequestError( request=req, current_response=resp, last_global_middleware_name=middleware.name, - ), - request=req, - response=resp, - ) + ) + except BoltUnhandledRequestError as e: + self._listener_runner.listener_error_handler.handle( + error=e, + request=req, + response=resp, + ) return resp self._framework_logger.warning(warning_unhandled_by_global_middleware(middleware.name, req)) return resp @@ -562,14 +565,17 @@ def middleware_next(): if resp is None: resp = BoltResponse(status=404, body={"error": "unhandled request"}) if self._raise_error_for_unhandled_request is True: - self._listener_runner.listener_error_handler.handle( - error=BoltUnhandledRequestError( + try: + raise BoltUnhandledRequestError( request=req, current_response=resp, - ), - request=req, - response=resp, - ) + ) + except BoltUnhandledRequestError as e: + self._listener_runner.listener_error_handler.handle( + error=e, + request=req, + response=resp, + ) return resp return self._handle_unmatched_requests(req, resp) except Exception as error: diff --git a/slack_bolt/app/async_app.py b/slack_bolt/app/async_app.py index 8e76bf4b6..7dca75b34 100644 --- a/slack_bolt/app/async_app.py +++ b/slack_bolt/app/async_app.py @@ -535,15 +535,18 @@ async def async_middleware_next(): # This should not be an intentional handling in usual use cases. resp = BoltResponse(status=404, body={"error": "no next() calls in middleware"}) if self._raise_error_for_unhandled_request is True: - await self._async_listener_runner.listener_error_handler.handle( - error=BoltUnhandledRequestError( + try: + raise BoltUnhandledRequestError( request=req, current_response=resp, last_global_middleware_name=middleware.name, - ), - request=req, - response=resp, - ) + ) + except BoltUnhandledRequestError as e: + await self._async_listener_runner.listener_error_handler.handle( + error=e, + request=req, + response=resp, + ) return resp self._framework_logger.warning(warning_unhandled_by_global_middleware(middleware.name, req)) return resp @@ -589,14 +592,17 @@ async def async_middleware_next(): if resp is None: resp = BoltResponse(status=404, body={"error": "unhandled request"}) if self._raise_error_for_unhandled_request is True: - await self._async_listener_runner.listener_error_handler.handle( - error=BoltUnhandledRequestError( + try: + raise BoltUnhandledRequestError( request=req, current_response=resp, - ), - request=req, - response=resp, - ) + ) + except BoltUnhandledRequestError as e: + await self._async_listener_runner.listener_error_handler.handle( + error=e, + request=req, + response=resp, + ) return resp return self._handle_unmatched_requests(req, resp) diff --git a/slack_bolt/error/__init__.py b/slack_bolt/error/__init__.py index 0e03032b1..d881fb188 100644 --- a/slack_bolt/error/__init__.py +++ b/slack_bolt/error/__init__.py @@ -23,3 +23,6 @@ def __init__( # type: ignore self.body = request.body if request is not None else {} self.current_response = current_response self.last_global_middleware_name = last_global_middleware_name + + def __str__(self) -> str: + return "unhandled request error" diff --git a/tests/slack_bolt/error/__init__.py b/tests/slack_bolt/error/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/slack_bolt/error/test_errors.py b/tests/slack_bolt/error/test_errors.py new file mode 100644 index 000000000..42a143d7c --- /dev/null +++ b/tests/slack_bolt/error/test_errors.py @@ -0,0 +1,19 @@ +from slack_bolt import BoltRequest +from slack_bolt.error import BoltUnhandledRequestError + + +class TestErrors: + def setup_method(self): + pass + + def teardown_method(self): + pass + + def test_say(self): + request = BoltRequest(body="foo=bar") + exception = BoltUnhandledRequestError( + request=request, + current_response={}, + last_global_middleware_name="last_middleware", + ) + assert str(exception) == "unhandled request error"