Skip to content

exceptions.py

Provides HTTPException class for error handling.

HTTPException dataclass

Bases: Response, Exception

Exception that also acts as a valid HTTP response.

Source code in jetweb/exceptions.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@dataclass
class HTTPException(Response, Exception):  # noqa: N818
    """
    Exception that also acts as a valid HTTP response.
    """
    status: int = 400

    def __post_init__(self):
        if not self.content:
            self.content = HTTPStatus(self.status).description
        super().__post_init__()

    @classmethod
    def from_exception(cls, exception: BaseException, catch_traceback: bool) -> HTTPException:
        """
        Convert any exception into an HTTPException.

        :param exception: Original exception.
        :param catch_traceback: Include traceback text if True.
        :returns: HTTPException object.
        """
        if isinstance(exception, cls):
            return exception
        return cls(
            content=format_exception(exception) if catch_traceback else None,
            status=500,
        )

from_exception(exception, catch_traceback) classmethod

Convert any exception into an HTTPException.

Parameters:

Name Type Description Default
exception BaseException

Original exception.

required
catch_traceback bool

Include traceback text if True.

required

Returns:

Type Description
HTTPException

HTTPException object.

Source code in jetweb/exceptions.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@classmethod
def from_exception(cls, exception: BaseException, catch_traceback: bool) -> HTTPException:
    """
    Convert any exception into an HTTPException.

    :param exception: Original exception.
    :param catch_traceback: Include traceback text if True.
    :returns: HTTPException object.
    """
    if isinstance(exception, cls):
        return exception
    return cls(
        content=format_exception(exception) if catch_traceback else None,
        status=500,
    )