|
1 | 1 | """Core Exceptions.""" |
2 | 2 |
|
3 | 3 | from collections.abc import Callable |
| 4 | +from typing import Any |
4 | 5 |
|
5 | 6 |
|
6 | 7 | class HassioError(Exception): |
7 | 8 | """Root exception.""" |
8 | 9 |
|
| 10 | + error_key: str | None = None |
| 11 | + message_template: str | None = None |
| 12 | + |
9 | 13 | def __init__( |
10 | 14 | self, |
11 | 15 | message: str | None = None, |
12 | 16 | logger: Callable[..., None] | None = None, |
| 17 | + *, |
| 18 | + extra_fields: dict[str, Any] | None = None, |
13 | 19 | ) -> None: |
14 | 20 | """Raise & log.""" |
| 21 | + self.extra_fields = extra_fields or {} |
| 22 | + |
| 23 | + if not message and self.message_template: |
| 24 | + message = ( |
| 25 | + self.message_template.format(**self.extra_fields) |
| 26 | + if self.extra_fields |
| 27 | + else self.message_template |
| 28 | + ) |
| 29 | + |
15 | 30 | if logger is not None and message is not None: |
16 | 31 | logger(message) |
17 | 32 |
|
@@ -235,8 +250,71 @@ class AddonConfigurationError(AddonsError): |
235 | 250 | """Error with add-on configuration.""" |
236 | 251 |
|
237 | 252 |
|
238 | | -class AddonsNotSupportedError(HassioNotSupportedError): |
239 | | - """Addons don't support a function.""" |
| 253 | +class AddonNotSupportedError(HassioNotSupportedError): |
| 254 | + """Addon doesn't support a function.""" |
| 255 | + |
| 256 | + |
| 257 | +class AddonNotSupportedArchitectureError(AddonNotSupportedError): |
| 258 | + """Addon does not support system due to architecture.""" |
| 259 | + |
| 260 | + error_key = "addon_not_supported_architecture_error" |
| 261 | + message_template = "Add-on {slug} not supported on this platform, supported architectures: {architectures}" |
| 262 | + |
| 263 | + def __init__( |
| 264 | + self, |
| 265 | + logger: Callable[..., None] | None = None, |
| 266 | + *, |
| 267 | + slug: str, |
| 268 | + architectures: list[str], |
| 269 | + ) -> None: |
| 270 | + """Initialize exception.""" |
| 271 | + super().__init__( |
| 272 | + None, |
| 273 | + logger, |
| 274 | + extra_fields={"slug": slug, "architectures": ", ".join(architectures)}, |
| 275 | + ) |
| 276 | + |
| 277 | + |
| 278 | +class AddonNotSupportedMachineTypeError(AddonNotSupportedError): |
| 279 | + """Addon does not support system due to machine type.""" |
| 280 | + |
| 281 | + error_key = "addon_not_supported_machine_type_error" |
| 282 | + message_template = "Add-on {slug} not supported on this machine, supported machine types: {machine_types}" |
| 283 | + |
| 284 | + def __init__( |
| 285 | + self, |
| 286 | + logger: Callable[..., None] | None = None, |
| 287 | + *, |
| 288 | + slug: str, |
| 289 | + machine_types: list[str], |
| 290 | + ) -> None: |
| 291 | + """Initialize exception.""" |
| 292 | + super().__init__( |
| 293 | + None, |
| 294 | + logger, |
| 295 | + extra_fields={"slug": slug, "machine_types": ", ".join(machine_types)}, |
| 296 | + ) |
| 297 | + |
| 298 | + |
| 299 | +class AddonNotSupportedHomeAssistantVersionError(AddonNotSupportedError): |
| 300 | + """Addon does not support system due to Home Assistant version.""" |
| 301 | + |
| 302 | + error_key = "addon_not_supported_home_assistant_version_error" |
| 303 | + message_template = "Add-on {slug} not supported on this system, requires Home Assistant version {version} or greater" |
| 304 | + |
| 305 | + def __init__( |
| 306 | + self, |
| 307 | + logger: Callable[..., None] | None = None, |
| 308 | + *, |
| 309 | + slug: str, |
| 310 | + version: str, |
| 311 | + ) -> None: |
| 312 | + """Initialize exception.""" |
| 313 | + super().__init__( |
| 314 | + None, |
| 315 | + logger, |
| 316 | + extra_fields={"slug": slug, "version": version}, |
| 317 | + ) |
240 | 318 |
|
241 | 319 |
|
242 | 320 | class AddonsJobError(AddonsError, JobException): |
@@ -319,10 +397,17 @@ def __init__( |
319 | 397 | self, |
320 | 398 | message: str | None = None, |
321 | 399 | logger: Callable[..., None] | None = None, |
| 400 | + *, |
322 | 401 | job_id: str | None = None, |
| 402 | + error: HassioError | None = None, |
323 | 403 | ) -> None: |
324 | 404 | """Raise & log, optionally with job.""" |
325 | | - super().__init__(message, logger) |
| 405 | + # Allow these to be set from another error here since APIErrors essentially wrap others to add a status |
| 406 | + self.error_key = error.error_key if error else None |
| 407 | + self.message_template = error.message_template if error else None |
| 408 | + super().__init__( |
| 409 | + message, logger, extra_fields=error.extra_fields if error else None |
| 410 | + ) |
326 | 411 | self.job_id = job_id |
327 | 412 |
|
328 | 413 |
|
|
0 commit comments