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/exception_middleware add exception middleware #1604

Merged
merged 19 commits into from
Aug 22, 2024
Merged

feature/exception_middleware add exception middleware #1604

merged 19 commits into from
Aug 22, 2024

Conversation

Rusich90
Copy link
Contributor

@Rusich90 Rusich90 commented Jul 18, 2024

fixes #1576, fixes #1617

Draft: Need create test and check linters

You can use ExceptionMiddleware like examples in issue

from pydantic import ValidationError

from faststream.broker.middlewares.exception import ExceptionMiddleware
from faststream.kafka import KafkaBroker

exception_middleware = ExceptionMiddleware()


@exception_middleware.add_handler(ZeroDivisionError)
async def handle_type_exception(exc: ZeroDivisionError): ...


@exception_middleware.add_handler(ValidationError)
async def handle_validation_error(exc: ValidationError): ...


broker = KafkaBroker("localhost:29092", middlewares=[exception_middleware])

or like this

from pydantic import ValidationError

from faststream.broker.middlewares.exception import ExceptionMiddleware
from faststream.kafka import KafkaBroker


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


async def handle_validation_error(exc: ValidationError): ...


exception_handlers = {
    ZeroDivisionError: handle_type_exception,
    ValidationError: handle_validation_error,
}

exception_middleware = ExceptionMiddleware(
    exception_handlers=exception_handlers
)

broker = KafkaBroker("localhost:29092", middlewares=[exception_middleware])

@Rusich90 Rusich90 marked this pull request as ready for review July 30, 2024 06:26
Copy link
Member

@Lancetnik Lancetnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test is too large, can u split it for specific ones?

faststream/broker/middlewares/exception.py Outdated Show resolved Hide resolved
@sheldygg
Copy link
Contributor

I made some improvements https://github.com/Rusich90/faststream_fork/pull/1

Lancetnik
Lancetnik previously approved these changes Aug 21, 2024
@Lancetnik Lancetnik requested a review from kumaranvpl August 21, 2024 19:54
@Lancetnik
Copy link
Member

@kumaranvpl can you, please, check the documentation and merge this one?

@Lancetnik Lancetnik added enhancement New feature or request Core Issues related to core FastStream functionality and affects to all brokers labels Aug 21, 2024
@Lancetnik Lancetnik self-assigned this Aug 21, 2024
@kumaranvpl
Copy link
Collaborator

@kumaranvpl can you, please, check the documentation and merge this one?

Proofread the docs. Will merge it now.

@kumaranvpl kumaranvpl enabled auto-merge August 22, 2024 05:22
@kumaranvpl kumaranvpl added this pull request to the merge queue Aug 22, 2024
Merged via the queue into airtai:main with commit e015bf8 Aug 22, 2024
29 checks passed
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
Projects
Archived in project
4 participants