Repository navigation
Conversation
IvanKirpichnikov
left a comment
There was a problem hiding this comment.
Hi. Sorry it took so long.
The idea of using to_async in __post_init__ didn’t really work for me. It ended up feeling a bit cumbersome. Let’s go back to your approach.
Second, why are you wrapping it in _build_fastdepends_model? Let’s do it in __init__ instead. In theory, nothing should break.
Third, the tests.
Let’s write e2e tests. The current tests are fragile because you monkeypatch a lot of things and call private methods.
I think it would be better to write e2e tests like this:
async def test_subscriber_exception_handler_true_suppresses_error(
self,
queue: str,
event: Event,
mock: MagicMock,
) -> None:
error = RuntimeError()
async def exception_handler(exc: BaseException) -> bool:
mock(exc)
return True
broker = self.get_broker()
args, kwargs = self.get_subscriber_params(queue, exception_handler=exception_handler)
@broker.subscriber(*args, **kwargs)
async def handler(msg: Any) -> NoReturn:
event.set()
raise error
async with self.patch_broker(broker) as br:
await asyncio.wait(
(
asyncio.create_task(br.publish(queue, None)),
asyncio.create_task(event.wait()),
),
timeout=self.timeout,
)
mock.assert_called_one_with(error)
IvanKirpichnikov
left a comment
There was a problem hiding this comment.
We also need to add another global exception handler at the FastStream level, which will also be taken into account when handling errors, as the very last one. Tests will need to be added for it.
| exc: BaseException | None, | ||
| _exc_tb: TracebackType | None, | ||
| ) -> bool: | ||
| if not isinstance(exc, Exception) or isinstance(exc, (StopConsume, SystemExit)): |
There was a problem hiding this comment.
Could you tell me why this particular condition is in place?
| self._outer_config.broker_exception_handler, | ||
| self._app_exception_handler, | ||
| ): | ||
| if handler is not None and await handler(exc, context__=context): |
There was a problem hiding this comment.
may remove inject context__
| # reserve place for context scope under the middlewares __aexit__ | ||
| # because middlewares should be exited before context scope release | ||
| message_scope = stack.enter_context(ExitStack()) | ||
| stack.push_async_exit(self._handle_exception) |
There was a problem hiding this comment.
This won’t work, will it?
| call("app", error), | ||
| ] | ||
|
|
||
| @pytest.mark.parametrize("handled_by", ("subscriber", "broker")) |
There was a problem hiding this comment.
I think we should split this test into two, because it’s hard to read the test with such parametrization.
| mock.assert_called_once_with("hello") | ||
| assert exc_info.value is error | ||
|
|
||
| @pytest.mark.parametrize("handler_level", ("subscriber", "broker")) |
There was a problem hiding this comment.
I think we should split this test into two, because it’s hard to read the test with such parametrization.
|
and pull main |
# Conflicts: # faststream/_internal/configs/broker.py
…-handlers # Conflicts: # faststream/_internal/endpoint/subscriber/usecase.py
Description
Adds configurable exception handlers following the proposal in:
#3095 (comment)
The subscriber handler runs first. If it is absent or returns False,
the broker handler runs. Both synchronous and asynchronous handlers
are supported.
This draft currently exposes the API for Kafka and adds shared
dispatch logic to SubscriberUsecase.consume(). Unhandled exceptions
retain the existing consume() behavior. Middleware logging and
acknowledgement behavior remain unchanged.
Related to #2945. Background-task error handling is not implemented
yet, so this draft does not resolve the original Redis scenario.
Validation
fallback results and dispatch through consume().
was removed, and to pass with it restored.
broker fallback, unhandled exceptions and continued processing.
Remaining work
Type of change
Checklist
just lint).just static-analysis).