Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add ExceptionMiddleware to handle processing exceptions #1576

Closed
Lancetnik opened this issue Jul 9, 2024 · 0 comments · Fixed by #1604
Closed

Feature: add ExceptionMiddleware to handle processing exceptions #1576

Lancetnik opened this issue Jul 9, 2024 · 0 comments · Fixed by #1604
Labels
Core Issues related to core FastStream functionality and affects to all brokers enhancement New feature or request good first issue Good for newcomers

Comments

@Lancetnik
Copy link
Member

We should provide users with a suitable mechanism to handle and process application exceptions like FastAPI does

I think the best way to make such mechanism suitable and flexable in the same time - implement it via special built-in middleware like in the following example:

Register handlers by constuctor:

from faststream import ExceptionMiddleware
from faststream.nats import NatsBroker

async def handle_value_exception(exc: ValueError): ...

async def handle_type_exception(exc: TypeError): ...

exception_handle_middleware = ExceptionMiddleware(
    {
        ValueError: handle_value_exception,
        TypeError: handle_type_exception,
    }
)

broker = NatsBroker(middlewares=(exception_handle_middleware,))

And register handlers by decorator:

from faststream import ExceptionMiddleware
from faststream.nats import NatsBroker

exception_handle_middleware = ExceptionMiddleware()

@exception_handle_middleware.add_handler(ValueError)
async def handle_value_exception(exc: ValueError): ...

@exception_handle_middleware.add_handler(TypeError)
async def handle_type_exception(exc: TypeError): ...

broker = NatsBroker(middlewares=(exception_handle_middleware,))
@Lancetnik Lancetnik added enhancement New feature or request good first issue Good for newcomers Core Issues related to core FastStream functionality and affects to all brokers labels Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core Issues related to core FastStream functionality and affects to all brokers enhancement New feature or request good first issue Good for newcomers
Projects
Archived in project
1 participant