Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
http_exceptions.py118 linesDownload Raw Back to aiohttp
1"""Low-level http related exceptions."""
2
3from textwrap import indent
4from typing import Optional, Union
5
6from .typedefs import _CIMultiDict
7
8__all__ = ("HttpProcessingError",)
9
10
11class HttpProcessingError(Exception):
12    """HTTP error.
13
14    Shortcut for raising HTTP errors with custom code, message and headers.
15
16    code: HTTP Error code.
17    message: (optional) Error message.
18    headers: (optional) Headers to be sent in response, a list of pairs
19    """
20
21    code = 0
22    message = ""
23    headers = None
24
25    def __init__(
26        self,
27        *,
28        code: Optional[int] = None,
29        message: str = "",
30        headers: Optional[_CIMultiDict] = None,
31    ) -> None:
32        if code is not None:
33            self.code = code
34        self.headers = headers
35        self.message = message
36
37    def __str__(self) -> str:
38        msg = indent(self.message, "  ")
39        return f"{self.code}, message:\n{msg}"
40
41    def __repr__(self) -> str:
42        return f"<{self.__class__.__name__}: {self.code}, message={self.message!r}>"
43
44
45class BadHttpMessage(HttpProcessingError):
46
47    code = 400
48    message = "Bad Request"
49
50    def __init__(self, message: str, *, headers: Optional[_CIMultiDict] = None) -> None:
51        super().__init__(message=message, headers=headers)
52        self.args = (message,)
53
54
55class HttpBadRequest(BadHttpMessage):
56
57    code = 400
58    message = "Bad Request"
59
60
61class PayloadEncodingError(BadHttpMessage):
62    """Base class for payload errors"""
63
64
65class ContentEncodingError(PayloadEncodingError):
66    """Content encoding error."""
67
68
69class TransferEncodingError(PayloadEncodingError):
70    """transfer encoding error."""
71
72
73class ContentLengthError(PayloadEncodingError):
74    """Not enough data to satisfy content length header."""
75
76
77class DecompressSizeError(PayloadEncodingError):
78    """Decompressed size exceeds the configured limit."""
79
80
81class LineTooLong(BadHttpMessage):
82    def __init__(
83        self,
84        line: Union[str, bytes],
85        limit: Union[str, int] = "Unknown",
86        actual_size: str = "Unknown",
87    ) -> None:
88        super().__init__(f"Got more than {limit} bytes when reading: {line!r}.")
89        self.args = (line, limit, actual_size)
90
91
92class InvalidHeader(BadHttpMessage):
93    def __init__(self, hdr: Union[bytes, str]) -> None:
94        hdr_s = hdr.decode(errors="backslashreplace") if isinstance(hdr, bytes) else hdr
95        super().__init__(f"Invalid HTTP header: {hdr!r}")
96        self.hdr = hdr_s
97        self.args = (hdr,)
98
99
100class BadStatusLine(BadHttpMessage):
101    def __init__(self, line: str = "", error: Optional[str] = None) -> None:
102        if not isinstance(line, str):
103            line = repr(line)
104        super().__init__(error or f"Bad status line {line!r}")
105        self.args = (line,)
106        self.line = line
107
108
109class BadHttpMethod(BadStatusLine):
110    """Invalid HTTP method in status line."""
111
112    def __init__(self, line: str = "", error: Optional[str] = None) -> None:
113        super().__init__(line, error or f"Bad HTTP method in status line {line!r}")
114
115
116class InvalidURLError(BadHttpMessage):
117    pass
118 
codekingpro/portable-devtools · Team Ai