FastStreamErrorHandler#
- class safir.kafka.FastStreamErrorHandler#
Bases:
objectError handler for FastStream that reports errors to external services.
Initializes and creates a FastStream ExceptionMiddleware handler that reports any uncaught exceptions in FastStream message handlers to any exception reporting services that are configured.
To report errors directly to Slack,
FastStreamErrorHandler.initialize_slackmust be called with a valid Slack webhook before any messages are handled.To report errors to Sentry, Sentry has to be initialized before any messages are handled.
Examples
Use this class when creating a
KafkaRouter.from safir.kafka import FastStreamErrorHandler from safir.sentry import initialize_sentry # If a Sentry DSN is provided in an environment variable, then # exceptions will be reported to Sentry. initialize_sentry() error_handler = FastStreamErrorHandler() kafka_router = KafkaRouter( middlewares=[error_handler.make_middleware()], **kafka_params, logger=logger, ) logger = get_logger("my-app") if config.slack.enabled: error_handler.initialize_slack("some_webhook", "my-app", logger)
Methods Summary
handle_error(exc)Report an exception to Slack and/or Sentry and re-raise it.
initialize_slack(hook_url, application, logger)Configure Slack alerting.
Create a FastStream Exception middleware to add to a broker.
Methods Documentation
- async handle_error(exc)#
Report an exception to Slack and/or Sentry and re-raise it.
This will called by the FastStream framework when the middleware generated by
FastStreamErrorHandler.make_middlewareis added to a router or broker.
- initialize_slack(hook_url, application, logger)#
Configure Slack alerting.
Until this function is called, all Slack alerting for uncaught exceptions will be disabled.
- Parameters:
hook_url (
str|SecretStr) – The URL of the incoming webhook to use to publish the message.application (
str) – Name of the application reporting an error.logger (
BoundLogger) – Logger to which to report errors sending messages to Slack.
- Return type:
- make_middleware()#
Create a FastStream Exception middleware to add to a broker.
- Return type:
ExceptionMiddleware