Coverage for dataexcept/network_exceptions.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-03 20:46 +0000

1"""Custom exceptions for network operations.""" 

2 

3from __future__ import annotations 

4 

5from .base import DataExceptError 

6 

7 

8class NetworkError(DataExceptError): 

9 """Base exception for network-related errors. 

10 

11 Example: 

12 >>> from dataexcept.network_exceptions import NetworkError 

13 >>> try: 

14 ... raise NetworkError("Something went wrong") 

15 ... except NetworkError: 

16 ... print("Caught network error") 

17 Caught network error 

18 """ 

19 

20 pass 

21 

22 

23class HostUnreachableError(NetworkError): 

24 """Raised when a remote host cannot be reached. 

25 

26 Example: 

27 >>> from dataexcept.network_exceptions import HostUnreachableError 

28 >>> try: 

29 ... raise HostUnreachableError("api.example.com") 

30 ... except HostUnreachableError as exc: 

31 ... print(exc) 

32 Host 'api.example.com' is unreachable 

33 """ 

34 

35 def __init__(self, host: str, message: str | None = None) -> None: 

36 """Initialize HostUnreachableError. 

37 

38 Args: 

39 host: Host address that could not be reached. 

40 message: Optional custom error message. 

41 """ 

42 self.host = host 

43 default = f"Host '{host}' is unreachable" 

44 super().__init__(message or default) 

45 

46 

47class ConnectionTimeoutError(NetworkError): 

48 """Raised when a network connection attempt times out. 

49 

50 Example: 

51 >>> from dataexcept.network_exceptions import ConnectionTimeoutError 

52 >>> try: 

53 ... raise ConnectionTimeoutError("api.example.com", 30) 

54 ... except ConnectionTimeoutError as exc: 

55 ... print(exc) 

56 Connection to 'api.example.com' timed out after 30 seconds 

57 """ 

58 

59 def __init__(self, host: str, timeout: float) -> None: 

60 """Initialize ConnectionTimeoutError. 

61 

62 Args: 

63 host: Host address. 

64 timeout: Timeout in seconds. 

65 """ 

66 self.host = host 

67 self.timeout = timeout 

68 msg = f"Connection to '{host}' timed out after {timeout} seconds" 

69 super().__init__(msg) 

70 

71 

72class ProtocolError(NetworkError): 

73 """Raised when an unexpected protocol error occurs. 

74 

75 Example: 

76 >>> from dataexcept.network_exceptions import ProtocolError 

77 >>> try: 

78 ... raise ProtocolError("HTTP", "Invalid status line") 

79 ... except ProtocolError as exc: 

80 ... print(exc) 

81 Protocol error in HTTP: Invalid status line 

82 """ 

83 

84 def __init__(self, protocol: str, details: str | None = None) -> None: 

85 """Initialize ProtocolError. 

86 

87 Args: 

88 protocol: Protocol name (e.g., HTTP). 

89 details: Optional additional details about the failure. 

90 """ 

91 self.protocol = protocol 

92 self.details = details 

93 msg = f"Protocol error in {protocol}" 

94 if details: 

95 msg += f": {details}" 

96 super().__init__(msg) 

97 

98 

99__all__ = [ 

100 "NetworkError", 

101 "HostUnreachableError", 

102 "ConnectionTimeoutError", 

103 "ProtocolError", 

104]