Skip to content

errors

stac_fastapi.api.errors

Error handling.

ErrorResponse

Bases: TypedDict

A JSON error response returned by the API.

The STAC API spec expects that code and description are both present in the payload.

Attributes:

  • code (str) –

    A code representing the error, semantics are up to implementor.

  • description (str) –

    A description of the error.

Source code in stac_fastapi/api/errors.py
35
36
37
38
39
40
41
42
43
44
45
46
47
class ErrorResponse(TypedDict):
    """A JSON error response returned by the API.

    The STAC API spec expects that `code` and `description` are both present in
    the payload.

    Attributes:
        code: A code representing the error, semantics are up to implementor.
        description: A description of the error.
    """

    code: str
    description: str

add_exception_handlers

add_exception_handlers(app: FastAPI, status_codes: Dict[Type[Exception], int]) -> None

Add exception handlers to the FastAPI application.

Parameters:

  • app (FastAPI) –

    the FastAPI application.

  • status_codes (Dict[Type[Exception], int]) –

    mapping between exceptions and status codes.

Returns:

  • None

    None

Source code in stac_fastapi/api/errors.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def add_exception_handlers(
    app: FastAPI, status_codes: Dict[Type[Exception], int]
) -> None:
    """Add exception handlers to the FastAPI application.

    Args:
        app: the FastAPI application.
        status_codes: mapping between exceptions and status codes.

    Returns:
        None
    """
    for exc, code in status_codes.items():
        app.add_exception_handler(exc, exception_handler_factory(code))

    # By default FastAPI will return 422 status codes for invalid requests
    # But the STAC api spec suggests returning a 400 in this case
    def request_validation_exception_handler(
        request: Request, exc: RequestValidationError
    ) -> JSONResponse:
        return JSONResponse(
            content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
            status_code=status.HTTP_400_BAD_REQUEST,
        )

    app.add_exception_handler(
        RequestValidationError, request_validation_exception_handler
    )

exception_handler_factory

exception_handler_factory(status_code: int) -> Callable

Create a FastAPI exception handler for a particular status code.

Parameters:

  • status_code (int) –

    HTTP status code.

Returns:

  • callable ( Callable ) –

    an exception handler.

Source code in stac_fastapi/api/errors.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def exception_handler_factory(status_code: int) -> Callable:
    """Create a FastAPI exception handler for a particular status code.

    Args:
        status_code: HTTP status code.

    Returns:
        callable: an exception handler.
    """

    def handler(request: Request, exc: Exception):
        """I handle exceptions!!."""
        logger.error(exc, exc_info=True)
        return JSONResponse(
            content=ErrorResponse(code=exc.__class__.__name__, description=str(exc)),
            status_code=status_code,
        )

    return handler