labthings_fastapi.problem_details
JSON representations of errors as per RFC 9457.
This module defines a model and supporting functions that help to create “Problem Details” objects to represent errors in HTTP responses.
Attributes
Classes
A model to describe an error that occurred in an invocation. |
Functions
|
Return a URL identifying a LabThings exception. |
Decorate a function to handle errors with a |
Module Contents
- class labthings_fastapi.problem_details.ProblemDetails(/, **data: Any)
Bases:
pydantic.BaseModelA model to describe an error that occurred in an invocation.
Create a new model by parsing and validating input data from keyword arguments.
Raises [
ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.selfis explicitly positional-only to allowselfas a field name.- model_config
Configuration for the model, should be a dictionary conforming to [
ConfigDict][pydantic.config.ConfigDict].
- classmethod from_exception(exc: BaseException) Self
Generate a
ProblemDetailsmodel from an exception instance.- Parameters:
exc – the exception instance to be described.
- Returns:
a
ProblemDetailsobject describingexc.
- json_response() fastapi.Response
Return a JSONResponse representing this object.
- Returns:
a
JSONResponseobject suitable for returning from a request handler.
- labthings_fastapi.problem_details.DOCS_URL = 'https://labthings-fastapi.readthedocs.io/en/latest/autoapi/labthings_fastapi/exceptions/index.html'
- labthings_fastapi.problem_details.PYTHON_DOCS_URL = 'https://docs.python.org/3/library/exceptions.html'
- labthings_fastapi.problem_details.docs_url(exc: type[BaseException]) str | None
Return a URL identifying a LabThings exception.
- Parameters:
exc – An exception class.
- Returns:
a URL pointing to that exception in the docs, or
Noneif the exception is not in this module.
- labthings_fastapi.problem_details.Params
- labthings_fastapi.problem_details.ReturnT
- labthings_fastapi.problem_details.exceptions_to_problem_details(logger: logging.Logger | None) Callable[[Callable[Params, ReturnT]], Callable[Params, ReturnT]]
Decorate a function to handle errors with a
ProblemDetailsresponse.- Parameters:
logger – the logger to use, or
Nonefor no logging.- Returns:
a decorator that handles errors by returning a
ProblemDetailsobject.