Debian Codegraph @

async_upnp_client/exceptions.py

raw symbols SCIP
1 """async_upnp_client.exceptions module."""
2
3 import asyncio
4 from enum import IntEnum
5 from typing import Any
6 from xml.etree import ElementTree as ET
7
8 import aiohttp
9
10 # pylint: disable=too-many-ancestors
11
12
13 class UpnpError(Exception):
14 """Base class for all errors raised by this library."""
15
16 def __init__(self, *args: Any, message: str | None = None, **_kwargs: Any) -> None:
17 """Initialize base UpnpError."""
18 super().__init__(*args, message)
19
20
21 class UpnpContentError(UpnpError):
22 """Content of UPnP response is invalid."""
23
24
25 class UpnpActionErrorCode(IntEnum):
26 """Error codes for UPnP Action errors."""
27
28 INVALID_ACTION = 401
29 INVALID_ARGS = 402
30 # (DO_NOT_USE) = 403
31 ACTION_FAILED = 501
32 ARGUMENT_VALUE_INVALID = 600
33 ARGUMENT_VALUE_OUT_OF_RANGE = 601
34 OPTIONAL_ACTION_NOT_IMPLEMENTED = 602
35 OUT_OF_MEMORY = 603
36 HUMAN_INTERVENTION_REQUIRED = 604
37 STRING_ARGUMENT_TOO_LONG = 605
38
39
40 class UpnpActionError(UpnpError):
41 """Server returned a SOAP Fault in response to an Action."""
42
43 def __init__(
44 self,
45 *args: Any,
46 error_code: int | None = None,
47 error_desc: str | None = None,
48 message: str | None = None,
49 **kwargs: Any,
50 ) -> None:
51 """Initialize from response body."""
52 if not message:
53 message = f"Received UPnP error {error_code} ({error_desc})"
54 super().__init__(*args, message=message, **kwargs)
55 self.error_code = error_code
56 self.error_desc = error_desc
57
58
59 class UpnpXmlParseError(UpnpContentError, ET.ParseError):
60 """UPnP response is not valid XML."""
61
62 def __init__(self, orig_err: ET.ParseError) -> None:
63 """Initialize from original ParseError, to match it."""
64 super().__init__(message=str(orig_err))
65 self.code = orig_err.code
66 self.position = orig_err.position
67
68
69 class UpnpValueError(UpnpContentError):
70 """Invalid value error."""
71
72 def __init__(self, name: str, value: Any) -> None:
73 """Initialize."""
74 super().__init__(message=f"Invalid value for {name}: '{value}'")
75 self.name = name
76 self.value = value
77
78
79 class UpnpSIDError(UpnpContentError):
80 """Missing Subscription Identifier from response."""
81
82
83 class UpnpXmlContentError(UpnpContentError):
84 """XML document does not have expected content."""
85
86
87 class UpnpCommunicationError(UpnpError, aiohttp.ClientError):
88 """Error occurred while communicating with the UPnP device ."""
89
90
91 class UpnpResponseError(UpnpCommunicationError):
92 """HTTP error code returned by the UPnP device."""
93
94 def __init__(
95 self,
96 *args: Any,
97 status: int,
98 headers: aiohttp.typedefs.LooseHeaders | None = None,
99 message: str | None = None,
100 **kwargs: Any,
101 ) -> None:
102 """Initialize."""
103 if not message:
104 message = f"Did not receive HTTP 200 but {status}"
105 super().__init__(*args, message=message, **kwargs)
106 self.status = status
107 self.headers = headers
108
109
110 class UpnpActionResponseError(UpnpActionError, UpnpResponseError):
111 """HTTP error code and UPnP error code.
112
113 UPnP errors are usually indicated with HTTP 500 (Internal Server Error) and
114 actual details in the response body as a SOAP Fault.
115 """
116
117 def __init__( # pylint: disable=too-many-arguments
118 self,
119 *args: Any,
120 status: int,
121 headers: aiohttp.typedefs.LooseHeaders | None = None,
122 error_code: int | None = None,
123 error_desc: str | None = None,
124 message: str | None = None,
125 **kwargs: Any,
126 ) -> None:
127 """Initialize."""
128 if not message:
129 message = f"Received HTTP error code {status}, UPnP error code {error_code} ({error_desc})"
130 super().__init__(
131 *args,
132 status=status,
133 headers=headers,
134 error_code=error_code,
135 error_desc=error_desc,
136 message=message,
137 **kwargs,
138 )
139
140
141 class UpnpClientResponseError(aiohttp.ClientResponseError, UpnpResponseError): # type: ignore
142 """HTTP response error with more details from aiohttp."""
143
144
145 class UpnpConnectionError(UpnpCommunicationError, aiohttp.ClientConnectionError):
146 """Error in the underlying connection to the UPnP device.
147
148 This could indicate that the device is offline.
149 """
150
151
152 class UpnpConnectionTimeoutError(UpnpConnectionError, aiohttp.ServerTimeoutError, asyncio.TimeoutError):
153 """Timeout while communicating with the device."""
154
155
156 class UpnpServerError(UpnpError):
157 """Error with a local server."""
158
159
160 class UpnpServerOSError(UpnpServerError, OSError):
161 """System-related error when starting a local server."""
162
163 def __init___(self, errno: int, strerror: str) -> None:
164 """Initialize simplified version of OSError."""
165 OSError.__init__(self, errno, strerror)