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

51 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 code = 400 

47 message = "Bad Request" 

48 

49 def __init__(self, message: str, *, headers: Optional[_CIMultiDict] = None) -> None: 

50 super().__init__(message=message, headers=headers) 

51 self.args = (message,) 

52 

53 

54class HttpBadRequest(BadHttpMessage): 

55 code = 400 

56 message = "Bad Request" 

57 

58 

59class PayloadEncodingError(BadHttpMessage): 

60 """Base class for payload errors""" 

61 

62 

63class ContentEncodingError(PayloadEncodingError): 

64 """Content encoding error.""" 

65 

66 

67class TransferEncodingError(PayloadEncodingError): 

68 """transfer encoding error.""" 

69 

70 

71class ContentLengthError(PayloadEncodingError): 

72 """Not enough data to satisfy content length header.""" 

73 

74 

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) 

83 

84 

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,) 

91 

92 

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 

98 

99 

100class BadHttpMethod(BadStatusLine): 

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

102 

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}") 

105 

106 

107class InvalidURLError(BadHttpMessage): 

108 pass