exceptions.py 3.6 KB

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