exceptions.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """Typed exceptions for the hth Python SDK."""
  2. from __future__ import annotations
  3. from typing import Any
  4. class HthError(Exception):
  5. """Base class for all SDK errors.
  6. Algorithm-facing code should catch this type only. Subclasses stay in
  7. `hth_sdk.advanced` for maintainers.
  8. """
  9. def __init__(self, *args: object, code: str = "", recoverable: bool = False, **_: object) -> None:
  10. super().__init__(*args)
  11. self.code = code or type(self).__name__
  12. self.recoverable = bool(recoverable)
  13. self._msg = str(self) if args else ""
  14. @property
  15. def message(self) -> str:
  16. return self._msg or str(self)
  17. @message.setter
  18. def message(self, value: str) -> None:
  19. self._msg = value
  20. class StubsNotGenerated(HthError):
  21. """Python protobuf stubs are missing; run tools/gen_python_stubs.py."""
  22. class TransportError(HthError):
  23. """Unix-socket / gRPC transport failure."""
  24. class ApiVersionMismatch(HthError):
  25. def __init__(self, found: str, expected: str) -> None:
  26. self.found = found
  27. self.expected = expected
  28. super().__init__(
  29. f"API version mismatch: daemon reported {found!r}, SDK expects {expected!r}"
  30. )
  31. class LeaseError(HthError):
  32. """Control lease rejected, missing, mismatched, or expired."""
  33. class LeaseExpired(LeaseError):
  34. pass
  35. class LeaseMismatch(LeaseError):
  36. pass
  37. class ActionRejected(HthError):
  38. def __init__(self, reason: str, message: str = "", command_id: str = "") -> None:
  39. self.reason = reason
  40. self.message = message
  41. self.command_id = command_id
  42. super().__init__(f"action rejected ({reason}): {message}".strip())
  43. class ConfirmationNotProven(HthError):
  44. """Raised only if a caller treats SET_ALL as CONFIRMED.
  45. Current Prod2U UART has no ACK. A successful serial write is UNCONFIRMED.
  46. """
  47. class DeviceEpochChanged(HthError):
  48. """Device epoch advanced (reconnect, daemon restart, profile change).
  49. Previous snapshots, leases, and in-flight outcomes are not truth for the
  50. new epoch. Callers must re-read capabilities/snapshot and re-acquire a lease.
  51. """
  52. def __init__(self, old_epoch: int, new_epoch: int, device_id: str = "") -> None:
  53. self.old_epoch = old_epoch
  54. self.new_epoch = new_epoch
  55. self.device_id = device_id
  56. super().__init__(
  57. f"device_epoch changed {old_epoch} -> {new_epoch} device={device_id!r}"
  58. )
  59. class DataGap(HthError):
  60. """Telemetry stream dropped snapshots; sequence is no longer contiguous."""
  61. def __init__(self, dropped_count: int = 0, resume_after_event_id: int = 0, **extra: Any) -> None:
  62. self.dropped_count = dropped_count
  63. self.resume_after_event_id = resume_after_event_id
  64. self.extra = extra
  65. super().__init__(
  66. f"telemetry data gap dropped={dropped_count} resume_after_event_id={resume_after_event_id}"
  67. )
  68. class NotConnected(HthError):
  69. """The requested role (observe/control) has no channel."""
  70. class InvalidResumePoint(HthError):
  71. """SubscribeEvents/SubscribeTelemetry resume_after_event_id is in the future."""
  72. def __init__(self, resume_after_event_id: int = 0, newest_event_id: int = 0, message: str = "") -> None:
  73. self.resume_after_event_id = resume_after_event_id
  74. self.newest_event_id = newest_event_id
  75. super().__init__(
  76. message or f"resume_after_event_id={resume_after_event_id} is after newest_event_id={newest_event_id}"
  77. )