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 fromSlackIgnoredException
) 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:
path (
str
)response_model (
Any
, default:<fastapi.datastructures.DefaultPlaceholder object at 0x7f293a8a8e00>
)response_description (
str
, default:'Successful Response'
)responses (
Optional
[Dict
[Union
[int
,str
],Dict
[str
,Any
]]], default:None
)response_model_include (
Union
[Set
[int
],Set
[str
],Dict
[int
,Any
],Dict
[str
,Any
],None
], default:None
)response_model_exclude (
Union
[Set
[int
],Set
[str
],Dict
[int
,Any
],Dict
[str
,Any
],None
], default:None
)response_model_by_alias (
bool
, default:True
)response_model_exclude_unset (
bool
, default:False
)response_model_exclude_defaults (
bool
, default:False
)response_model_exclude_none (
bool
, default:False
)include_in_schema (
bool
, default:True
)response_class (
Union
[Type
[Response
],DefaultPlaceholder
], default:<fastapi.datastructures.DefaultPlaceholder object at 0x7f293a8a8e30>
)dependency_overrides_provider (
Optional
[Any
], default:None
)generate_unique_id_function (
Union
[Callable
[[APIRoute
],str
],DefaultPlaceholder
], default:<fastapi.datastructures.DefaultPlaceholder object at 0x7f293a8a8e60>
)
Methods Summary
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.
- 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
|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: