Coverage for /pythoncovmergedfiles/medio/medio/src/aiohttp/aiohttp/http_exceptions.py: 76%
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
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 msg = indent(self.message, " ")
39 return f"{self.code}, message:\n{msg}"
41 def __repr__(self) -> str:
42 return f"<{self.__class__.__name__}: {self.code}, message={self.message!r}>"
45class 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):
55 code = 400
56 message = "Bad Request"
59class PayloadEncodingError(BadHttpMessage):
60 """Base class for payload errors"""
63class ContentEncodingError(PayloadEncodingError):
64 """Content encoding error."""
67class TransferEncodingError(PayloadEncodingError):
68 """transfer encoding error."""
71class ContentLengthError(PayloadEncodingError):
72 """Not enough data to satisfy content length header."""
75class LineTooLong(BadHttpMessage):
76 def __init__(
77 self, line: str, limit: str = "Unknown", actual_size: str = "Unknown"
78 ) -> None:
79 super().__init__(
80 f"Got more than {limit} bytes ({actual_size}) when reading {line}."
81 )
82 self.args = (line, limit, actual_size)
85class InvalidHeader(BadHttpMessage):
86 def __init__(self, hdr: Union[bytes, str]) -> None:
87 hdr_s = hdr.decode(errors="backslashreplace") if isinstance(hdr, bytes) else hdr
88 super().__init__(f"Invalid HTTP header: {hdr!r}")
89 self.hdr = hdr_s
90 self.args = (hdr,)
93class BadStatusLine(BadHttpMessage):
94 def __init__(self, line: str = "", error: Optional[str] = None) -> None:
95 super().__init__(error or f"Bad status line {line!r}")
96 self.args = (line,)
97 self.line = line
100class BadHttpMethod(BadStatusLine):
101 """Invalid HTTP method in status line."""
103 def __init__(self, line: str = "", error: Optional[str] = None) -> None:
104 super().__init__(line, error or f"Bad HTTP method in status line {line!r}")
107class InvalidURLError(BadHttpMessage):
108 pass