SlackRouteErrorHandler#

class safir.slack.webhook.SlackRouteErrorHandler(path, endpoint, *, response_model=<fastapi.datastructures.DefaultPlaceholder object>, status_code=None, tags=None, dependencies=None, summary=None, description=None, response_description='Successful Response', responses=None, deprecated=None, name=None, methods=None, operation_id=None, response_model_include=None, response_model_exclude=None, response_model_by_alias=True, response_model_exclude_unset=False, response_model_exclude_defaults=False, response_model_exclude_none=False, include_in_schema=True, response_class=<fastapi.datastructures.DefaultPlaceholder object>, dependency_overrides_provider=None, callbacks=None, openapi_extra=None, generate_unique_id_function=<fastapi.datastructures.DefaultPlaceholder object>)#

Bases: APIRoute

Custom fastapi.routing.APIRoute that reports exceptions to Slack.

Dynamically wrap FastAPI route handlers in an exception handler that reports uncaught exceptions (other than fastapi.HTTPException, fastapi.exceptions.RequestValidationError, starlette.exceptions.HTTPException, and exceptions inheriting from SlackIgnoredException) to Slack.

This class must be initialized by calling its initialize method to send alerts. Until that has been done, it will silently do nothing.

Examples

Specify this class when creating a router. All uncaught exceptions from handlers managed by that router will be reported to Slack, if Slack alerts are configured.

router = APIRouter(route_class=SlackRouteErrorHandler)

Notes

Based on this StackOverflow question.

Parameters:

Methods Summary

get_route_handler()

Wrap route handler with an exception handler.

initialize(hook_url, application, logger)

Configure Slack alerting.

Methods Documentation

get_route_handler()#

Wrap route handler with an exception handler. :rtype: Callable[[Request], Coroutine[Any, Any, Response]]

classmethod initialize(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) – 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:

None