Skip to content

HTTPException class

This is the reference for the main object HTTPException that contains all the parameters, attributes and functions.

esmerald.HTTPException

HTTPException(*args, detail=None, status_code=None, headers=None, **extra)

Bases: HTTPException, EsmeraldAPIException

Base of all Esmerald execeptions.

Tip

For an implementation of a custom exception that you need to be thrown by Esmerald, it is advised to subclass HTTPException.

PARAMETER DESCRIPTION
*args

The args passed to the exception.

TYPE: Any DEFAULT: ()

detail

A string text with the details of the error being thrown.

TYPE: Optional[str] DEFAULT: None

status_code

An integer with the status code to be raised.

TYPE: Optional[int] DEFAULT: None

headers

Any python dictionary containing headers.

TYPE: Optional[Dict[str, Any]] DEFAULT: None

**extra

Any extra information used by the exception.

TYPE: Any DEFAULT: {}

Source code in esmerald/exceptions.py
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 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
 99
100
101
102
103
104
105
106
107
108
109
110
111
def __init__(
    self,
    *args: Annotated[
        Any,
        Doc(
            """
            The args passed to the exception.
            """
        ),
    ],
    detail: Annotated[
        Optional[str],
        Doc(
            """
            A string text with the details of the error being thrown.
            """
        ),
    ] = None,
    status_code: Annotated[
        Optional[int],
        Doc(
            """
            An integer with the status code to be raised.
            """
        ),
    ] = None,
    headers: Annotated[
        Optional[Dict[str, Any]],
        Doc(
            """
            Any python dictionary containing headers.
            """
        ),
    ] = None,
    **extra: Annotated[
        Any,
        Doc(
            """
            Any extra information used by the exception.
            """
        ),
    ],
) -> None:
    detail = detail or getattr(self, "detail", None)
    status_code = status_code or getattr(self, "status_code", None)
    if not detail:
        detail = args[0] if args else HTTPStatus(status_code or self.status_code).phrase
        args = args[1:]
    super().__init__(status_code=status_code, detail=detail, headers=headers)
    self.detail = detail
    self.headers = headers
    self.args = (f"{self.status_code}: {self.detail}", *args)
    self.extra = extra

status_code class-attribute instance-attribute

detail instance-attribute

detail = detail

headers instance-attribute

headers = headers

args instance-attribute

args = (f'{status_code}: {detail}', *args)

extra instance-attribute

extra = extra