-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrors.py
More file actions
292 lines (210 loc) · 8.31 KB
/
Copy patherrors.py
File metadata and controls
292 lines (210 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""Public exception hierarchy for the reCamera Python AI kit.
The kit is expected to run unattended on an embedded device. A caller must be
able to distinguish a bad model/input from a transient transport failure or a
busy hardware resource without parsing human-readable strings. Every public
exception therefore carries a stable ``code`` and an ``operation`` while its
message remains useful in logs.
The hierarchy is intentionally small. Backend-specific details (an errno, an
HTTP status, or a native SDK return code) belong in ``details`` and should not
become new application-facing exception classes.
"""
from __future__ import annotations
import copy
from collections.abc import Sequence
from dataclasses import dataclass
from types import MappingProxyType
from typing import Any, Mapping, Optional
class _FrozenSequence(Sequence[Any]):
"""Immutable sequence that still compares naturally with JSON lists.
Subclassing ``list`` is insufficient: ``list.append(value, item)`` can
bypass overridden mutation methods. Composition avoids that escape hatch
while preserving iteration, indexing and ``== [..]`` compatibility.
"""
__slots__ = ("_items",)
def __init__(self, items) -> None:
self._items = tuple(items)
def __getitem__(self, index):
return self._items[index]
def __len__(self) -> int:
return len(self._items)
def __iter__(self):
return iter(self._items)
def __repr__(self) -> str:
return repr(list(self._items))
def __eq__(self, other: object) -> bool:
if isinstance(other, Sequence) and not isinstance(other, (str, bytes, bytearray)):
return tuple(self._items) == tuple(other)
return False
@staticmethod
def _immutable(*_args, **_kwargs):
raise TypeError("error detail sequences are read-only")
append = _immutable
clear = _immutable
extend = _immutable
insert = _immutable
pop = _immutable
remove = _immutable
reverse = _immutable
sort = _immutable
def _freeze(value: Any) -> Any:
"""Recursively freeze a copied JSON-like diagnostic value."""
if isinstance(value, Mapping):
return MappingProxyType({str(key): _freeze(item)
for key, item in value.items()})
if isinstance(value, list):
return _FrozenSequence(_freeze(item) for item in value)
if isinstance(value, tuple):
return tuple(_freeze(item) for item in value)
if isinstance(value, (set, frozenset)):
return tuple(_freeze(item) for item in value)
return value
def _thaw(value: Any) -> Any:
"""Return ordinary JSON-compatible containers for serialization."""
if isinstance(value, Mapping):
return {str(key): _thaw(item) for key, item in value.items()}
if isinstance(value, (list, tuple, _FrozenSequence)):
return [_thaw(item) for item in value]
return value
@dataclass(frozen=True)
class ErrorContext:
"""Machine-readable context attached to :class:`KitError`.
``details`` is copied and made read-only so code catching an exception can
safely pass the context to another thread or serialize it for diagnostics.
Values should be JSON-compatible and must not contain credentials.
"""
operation: str
code: str
retryable: bool
details: Mapping[str, Any]
class KitError(Exception):
"""Base class for all documented kit failures.
Parameters:
message: Human-readable diagnosis. It is safe to show this to an app
developer, but it is not a stable value for program logic.
operation: Stable name of the operation that failed, for example
``"model.load"`` or ``"frame.acquire"``.
code: Stable short error code. Subclasses provide a useful default.
retryable: Whether retrying later *may* succeed without changing input.
details: Optional non-secret backend diagnostics.
"""
default_code = "kit_error"
def __init__(
self,
message: str,
*,
operation: str = "unknown",
code: Optional[str] = None,
retryable: bool = False,
details: Optional[Mapping[str, Any]] = None,
) -> None:
super().__init__(str(message))
copied = copy.deepcopy(dict(details or {}))
self.context = ErrorContext(
operation=str(operation),
code=str(code or self.default_code),
retryable=bool(retryable),
details=_freeze(copied),
)
@property
def operation(self) -> str:
"""Stable operation name associated with the failure."""
return self.context.operation
@property
def code(self) -> str:
"""Stable error code suitable for application branching."""
return self.context.code
@property
def retryable(self) -> bool:
"""Whether a later retry may succeed without changing the request."""
return self.context.retryable
@property
def details(self) -> Mapping[str, Any]:
"""Read-only, non-secret backend details."""
return self.context.details
def as_dict(self) -> dict[str, Any]:
"""Return a JSON-compatible representation for status endpoints."""
return {
"type": type(self).__name__,
"message": str(self),
"operation": self.operation,
"code": self.code,
"retryable": self.retryable,
"details": _thaw(self.details),
}
class ConfigurationError(KitError, ValueError):
"""An application manifest, option, or model declaration is invalid."""
default_code = "invalid_configuration"
class AdapterError(KitError, RuntimeError):
"""A frame, result, control, audio, or image backend failed."""
default_code = "adapter_error"
class ImageOperationError(AdapterError):
"""RGA or software image transformation failed."""
default_code = "image_operation_failed"
class TransportError(AdapterError):
"""A local socket, HTTP compatibility endpoint, or subprocess failed."""
default_code = "transport_error"
class DeviceControlError(AdapterError):
"""A device-control request was rejected or returned invalid state."""
default_code = "device_control_error"
class CapabilityError(KitError):
"""A required device capability is absent or has not been verified."""
default_code = "capability_unavailable"
class ResourceBusyError(KitError, RuntimeError):
"""A hardware resource is owned by another workflow."""
default_code = "resource_busy"
class ResourceTimeoutError(ResourceBusyError, TimeoutError):
"""A resource did not become safe to acquire before the deadline."""
default_code = "resource_timeout"
class InferenceError(KitError, RuntimeError):
"""Base class for model loading, input validation, and inference errors."""
default_code = "inference_error"
class ModelLoadError(InferenceError):
"""An RKNN model could not be loaded or its runtime could not initialize."""
default_code = "model_load_failed"
class InputValidationError(InferenceError, ValueError):
"""An inference input does not match the model/session contract."""
default_code = "invalid_inference_input"
class BufferReleasedError(KitError, RuntimeError):
"""A borrowed or explicitly released image buffer was accessed."""
default_code = "buffer_released"
def wrap_error(
exc: BaseException,
error_type: type[KitError],
message: str,
*,
operation: str,
code: Optional[str] = None,
retryable: bool = False,
details: Optional[Mapping[str, Any]] = None,
) -> KitError:
"""Create a typed public error while retaining the backend exception.
Use it as ``raise wrap_error(...) from exc``. Keeping ``__cause__`` gives
detailed tracebacks to developers without exposing backend-specific types
as part of the public API.
"""
error = error_type(
message,
operation=operation,
code=code,
retryable=retryable,
details=details,
)
error.__cause__ = exc
return error
__all__ = [
"AdapterError",
"BufferReleasedError",
"CapabilityError",
"ConfigurationError",
"DeviceControlError",
"ErrorContext",
"InferenceError",
"ImageOperationError",
"InputValidationError",
"KitError",
"ModelLoadError",
"ResourceBusyError",
"ResourceTimeoutError",
"TransportError",
"wrap_error",
]