"""Typed exceptions for the hth Python SDK.""" from __future__ import annotations from typing import Any class HthError(Exception): """Base class for all SDK errors. Algorithm-facing code should catch this type only. Subclasses stay in `hth_sdk.advanced` for maintainers. """ def __init__( self, *args: object, code: str = "", recoverable: bool = False, suggestion: str = "", **_: object, ) -> None: super().__init__(*args) self.code = code or type(self).__name__ self.recoverable = bool(recoverable) self.suggestion = str(suggestion or "") self._msg = str(self) if args else "" @property def message(self) -> str: return self._msg or str(self) @message.setter def message(self, value: str) -> None: self._msg = value class StubsNotGenerated(HthError): """Python protobuf stubs are missing; run tools/gen_python_stubs.py.""" class TransportError(HthError): """Unix-socket / gRPC transport failure.""" class ApiVersionMismatch(HthError): def __init__(self, found: str, expected: str) -> None: self.found = found self.expected = expected super().__init__( f"API version mismatch: daemon reported {found!r}, SDK expects {expected!r}" ) class LeaseError(HthError): """Control lease rejected, missing, mismatched, or expired.""" class LeaseExpired(LeaseError): pass class LeaseMismatch(LeaseError): pass class ActionRejected(HthError): def __init__(self, reason: str, message: str = "", command_id: str = "") -> None: self.reason = reason self.message = message self.command_id = command_id super().__init__(f"action rejected ({reason}): {message}".strip()) class ConfirmationNotProven(HthError): """Raised only if a caller treats SET_ALL as CONFIRMED. Current Prod2U UART has no ACK. A successful serial write is UNCONFIRMED. """ class DeviceEpochChanged(HthError): """Device epoch advanced (reconnect, daemon restart, profile change). Previous snapshots, leases, and in-flight outcomes are not truth for the new epoch. Callers must re-read capabilities/snapshot and re-acquire a lease. """ def __init__(self, old_epoch: int, new_epoch: int, device_id: str = "") -> None: self.old_epoch = old_epoch self.new_epoch = new_epoch self.device_id = device_id super().__init__( f"device_epoch changed {old_epoch} -> {new_epoch} device={device_id!r}" ) class DataGap(HthError): """Telemetry stream dropped snapshots; sequence is no longer contiguous.""" def __init__(self, dropped_count: int = 0, resume_after_event_id: int = 0, **extra: Any) -> None: self.dropped_count = dropped_count self.resume_after_event_id = resume_after_event_id self.extra = extra super().__init__( f"telemetry data gap dropped={dropped_count} resume_after_event_id={resume_after_event_id}" ) class NotConnected(HthError): """The requested role (observe/control) has no channel.""" class InvalidResumePoint(HthError): """SubscribeEvents/SubscribeTelemetry resume_after_event_id is in the future.""" def __init__(self, resume_after_event_id: int = 0, newest_event_id: int = 0, message: str = "") -> None: self.resume_after_event_id = resume_after_event_id self.newest_event_id = newest_event_id super().__init__( message or f"resume_after_event_id={resume_after_event_id} is after newest_event_id={newest_event_id}" )