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
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-03 20:46 +0000
1"""Custom exceptions for network operations."""
3from __future__ import annotations
5from .base import DataExceptError
8class NetworkError(DataExceptError):
9 """Base exception for network-related errors.
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 """
20 pass
23class HostUnreachableError(NetworkError):
24 """Raised when a remote host cannot be reached.
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 """
35 def __init__(self, host: str, message: str | None = None) -> None:
36 """Initialize HostUnreachableError.
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)
47class ConnectionTimeoutError(NetworkError):
48 """Raised when a network connection attempt times out.
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 """
59 def __init__(self, host: str, timeout: float) -> None:
60 """Initialize ConnectionTimeoutError.
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)
72class ProtocolError(NetworkError):
73 """Raised when an unexpected protocol error occurs.
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 """
84 def __init__(self, protocol: str, details: str | None = None) -> None:
85 """Initialize ProtocolError.
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)
99__all__ = [
100 "NetworkError",
101 "HostUnreachableError",
102 "ConnectionTimeoutError",
103 "ProtocolError",
104]