exceptions.py 2.9 KB

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