Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/aiohttp/http_exceptions.py: 59%
49 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:56 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:56 +0000
1"""Low-level http related exceptions."""
4from typing import Optional, Union
6from .typedefs import _CIMultiDict
8__all__ = ("HttpProcessingError",)
11class HttpProcessingError(Exception):
12 """HTTP error.
14 Shortcut for raising HTTP errors with custom code, message and headers.
16 code: HTTP Error code.
17 message: (optional) Error message.
18 headers: (optional) Headers to be sent in response, a list of pairs
19 """
21 code = 0
22 message = ""
23 headers = None
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
37 def __str__(self) -> str:
38 return f"{self.code}, message={self.message!r}"
40 def __repr__(self) -> str:
41 return f"<{self.__class__.__name__}: {self}>"
44class BadHttpMessage(HttpProcessingError):
46 code = 400
47 message = "Bad Request"
49 def __init__(self, message: str, *, headers: Optional[_CIMultiDict] = None) -> None:
50 super().__init__(message=message, headers=headers)
51 self.args = (message,)
54class 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 for satisfy content length header."""
76class LineTooLong(BadHttpMessage):
77 def __init__(
78 self, line: str, limit: str = "Unknown", actual_size: str = "Unknown"
79 ) -> None:
80 super().__init__(
81 f"Got more than {limit} bytes ({actual_size}) when reading {line}."
82 )
83 self.args = (line, limit, actual_size)
86class InvalidHeader(BadHttpMessage):
87 def __init__(self, hdr: Union[bytes, str]) -> None:
88 if isinstance(hdr, bytes):
89 hdr = hdr.decode("utf-8", "surrogateescape")
90 super().__init__(f"Invalid HTTP Header: {hdr}")
91 self.hdr = hdr
92 self.args = (hdr,)
95class BadStatusLine(BadHttpMessage):
96 def __init__(self, line: str = "") -> None:
97 if not isinstance(line, str):
98 line = repr(line)
99 super().__init__(f"Bad status line {line!r}")
100 self.args = (line,)
101 self.line = line
104class InvalidURLError(BadHttpMessage):
105 pass