Skip to content

Commit

Permalink
Improve the default handler when raise_error_for_unhandled_request is…
Browse files Browse the repository at this point in the history
… true (#885)
  • Loading branch information
seratch authored Apr 18, 2023
1 parent f8c1b86 commit 7bf508f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
30 changes: 18 additions & 12 deletions slack_bolt/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
30 changes: 18 additions & 12 deletions slack_bolt/app/async_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions slack_bolt/error/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Empty file.
19 changes: 19 additions & 0 deletions tests/slack_bolt/error/test_errors.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 7bf508f

Please sign in to comment.