Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/aiohttp/http_exceptions.py: 62%

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

55 statements  

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, line: str, limit: str = "Unknown", actual_size: str = "Unknown" 

84 ) -> None: 

85 super().__init__( 

86 f"Got more than {limit} bytes ({actual_size}) when reading {line}." 

87 ) 

88 self.args = (line, limit, actual_size) 

89 

90 

91class InvalidHeader(BadHttpMessage): 

92 def __init__(self, hdr: Union[bytes, str]) -> None: 

93 hdr_s = hdr.decode(errors="backslashreplace") if isinstance(hdr, bytes) else hdr 

94 super().__init__(f"Invalid HTTP header: {hdr!r}") 

95 self.hdr = hdr_s 

96 self.args = (hdr,) 

97 

98 

99class BadStatusLine(BadHttpMessage): 

100 def __init__(self, line: str = "", error: Optional[str] = None) -> None: 

101 if not isinstance(line, str): 

102 line = repr(line) 

103 super().__init__(error or f"Bad status line {line!r}") 

104 self.args = (line,) 

105 self.line = line 

106 

107 

108class BadHttpMethod(BadStatusLine): 

109 """Invalid HTTP method in status line.""" 

110 

111 def __init__(self, line: str = "", error: Optional[str] = None) -> None: 

112 super().__init__(line, error or f"Bad HTTP method in status line {line!r}") 

113 

114 

115class InvalidURLError(BadHttpMessage): 

116 pass