Skip to content

fastapi-buzz Reference

fastapi_buzz

FastAPIBuzz

Bases: Buzz

build_error_handler classmethod
build_error_handler(
    *tasks: FastAPIBuzzTask,
) -> tuple[type[Self], HTTPExceptionHandler]

Build an error handling function specifically for FastAPIBuzz errors.

The resulting error handler function will catch any FastAPIBuzz exceptions and raise an HTTPException that encapsulates the details of the original.

Descendants of the exception will also be caught and handled.

Example usage:

app.add_exception_handler(*FastAPIBuzz.build_error_handler())

You may also add extra tasks that will operate on the error prior raising the final HTTPException:

app.register_error_handler(*FastAPIBuzz.build_error_handler(print, lambda e: jawa(e)))

This latter example will print the error to stdout and also call the jawa() function with the error prior to raising the final HTTPException.

build_json_response
build_json_response(
    status_code: int | None = None,
    message: str | None = None,
    headers: dict[str, str] | None = None,
    **kwargs: Any,
) -> JSONResponse

Build a json response from the FastAPI error in form that is compatible with FastAPI.

Keyword arguments allow custom data to be included in the rsponse's body when it is built.

If debug is set, the stringified exception will be included in the response body as well. For apps running in production or publicly should not run in debug mode as this could expose internal information to clients.

Args:

status_code: The status code to include in the response. If not supplied, use instance status_code.
message:     The message to include in the response. If not supplied, use instance message. If debug
             is set and the base_message is not None, use the base_message instead. This is important,
             because if the exception was raised by `handle_errors`, the full message will return
             details of the handled exception as well as the base message. This information should
             probably not be returned to the client.
headers:     The headers to attach to the response. If not supplied, use instance headers. If the
             instance has no headers, don't include headers.
kwargs:      Additional fields that should be set in the response body. These must be JSON
             serializable.