Coverage for /pythoncovmergedfiles/medio/medio/src/aiohttp/aiohttp/http_exceptions.py: 91%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Low-level http related exceptions."""
3from textwrap import indent
5from multidict import CIMultiDict
7__all__ = ("HttpProcessingError",)
10class HttpProcessingError(Exception):
11 """HTTP error.
13 Shortcut for raising HTTP errors with custom code, message and headers.
15 code: HTTP Error code.
16 message: (optional) Error message.
17 headers: (optional) Headers to be sent in response, a list of pairs
18 """
20 code = 0
21 message = ""
22 headers = None
24 def __init__(
25 self,
26 *,
27 code: int | None = None,
28 message: str = "",
29 headers: CIMultiDict[str] | None = None,
30 ) -> None:
31 if code is not None:
32 self.code = code
33 self.headers = headers
34 self.message = message
36 def __str__(self) -> str:
37 msg = indent(self.message, " ")
38 return f"{self.code}, message:\n{msg}"
40 def __repr__(self) -> str:
41 return f"<{self.__class__.__name__}: {self.code}, message={self.message!r}>"
44class BadHttpMessage(HttpProcessingError):
45 code = 400
46 message = "Bad Request"
48 def __init__(
49 self, message: str, *, headers: CIMultiDict[str] | None = None
50 ) -> None:
51 super().__init__(message=message, headers=headers)
52 self.args = (message,)
55class HttpBadRequest(BadHttpMessage):
56 code = 400
57 message = "Bad Request"
60class PayloadEncodingError(BadHttpMessage):
61 """Base class for payload errors"""
64class ContentEncodingError(PayloadEncodingError):
65 """Content encoding error."""
68class TransferEncodingError(PayloadEncodingError):
69 """transfer encoding error."""
72class ContentLengthError(PayloadEncodingError):
73 """Not enough data to satisfy content length header."""
76class LineTooLong(BadHttpMessage):
77 args: tuple[bytes, int]
79 def __init__(self, line: bytes, limit: int) -> None:
80 super().__init__(f"Got more than {limit} bytes when reading: {line!r}.")
81 self.args = (line, limit)
84class InvalidHeader(BadHttpMessage):
85 args: tuple[bytes | str]
87 def __init__(self, hdr: bytes | str) -> None:
88 hdr_s = hdr.decode(errors="backslashreplace") if isinstance(hdr, bytes) else hdr
89 super().__init__(f"Invalid HTTP header: {hdr!r}")
90 self.hdr = hdr_s
91 self.args = (hdr,)
94class BadStatusLine(BadHttpMessage):
95 args: tuple[str]
97 def __init__(self, line: str = "", error: str | None = None) -> None:
98 super().__init__(error or f"Bad status line {line!r}")
99 self.args = (line,)
100 self.line = line
103class BadHttpMethod(BadStatusLine):
104 """Invalid HTTP method in status line."""
106 def __init__(self, line: str = "", error: str | None = None) -> None:
107 if error is None and line.startswith("\x16\x03"):
108 error = "Received HTTPS traffic on an HTTP port"
109 super().__init__(line, error or f"Bad HTTP method in status line {line!r}")
112class InvalidURLError(BadHttpMessage):
113 pass