Home / Class/ HTTPException Class — fastapi Architecture

HTTPException Class — fastapi Architecture

Architecture documentation for the HTTPException class in exceptions.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  53e07af2_3e5c_ea1f_ee6c_abc9792bf48b["HTTPException"]
  01c652c5_d85c_f45e_848e_412c94ea4172["exceptions.py"]
  53e07af2_3e5c_ea1f_ee6c_abc9792bf48b -->|defined in| 01c652c5_d85c_f45e_848e_412c94ea4172
  f6872f86_1345_04a3_eed4_2f571bd8af4d["__init__()"]
  53e07af2_3e5c_ea1f_ee6c_abc9792bf48b -->|method| f6872f86_1345_04a3_eed4_2f571bd8af4d

Relationship Graph

Source Code

fastapi/exceptions.py lines 17–83

class HTTPException(StarletteHTTPException):
    """
    An HTTP exception you can raise in your own code to show errors to the client.

    This is for client errors, invalid authentication, invalid data, etc. Not for server
    errors in your code.

    Read more about it in the
    [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/).

    ## Example

    ```python
    from fastapi import FastAPI, HTTPException

    app = FastAPI()

    items = {"foo": "The Foo Wrestlers"}


    @app.get("/items/{item_id}")
    async def read_item(item_id: str):
        if item_id not in items:
            raise HTTPException(status_code=404, detail="Item not found")
        return {"item": items[item_id]}
    ```
    """

    def __init__(
        self,
        status_code: Annotated[
            int,
            Doc(
                """
                HTTP status code to send to the client.

                Read more about it in the
                [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/#use-httpexception)
                """
            ),
        ],
        detail: Annotated[
            Any,
            Doc(
                """
                Any data to be sent to the client in the `detail` key of the JSON
                response.

                Read more about it in the
                [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/#use-httpexception)
                """
            ),
        ] = None,
        headers: Annotated[
            Optional[Mapping[str, str]],
            Doc(
                """
                Any headers to send to the client in the response.

                Read more about it in the
                [FastAPI docs for Handling Errors](https://fastapi.tiangolo.com/tutorial/handling-errors/#add-custom-headers)

                """
            ),
        ] = None,
    ) -> None:
        super().__init__(status_code=status_code, detail=detail, headers=headers)

Domain

Frequently Asked Questions

What is the HTTPException class?
HTTPException is a class in the fastapi codebase, defined in fastapi/exceptions.py.
Where is HTTPException defined?
HTTPException is defined in fastapi/exceptions.py at line 17.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free