Files
seismo-relay/sfm/device_access/errors.py
T
serversdownandClaude Opus 5.5 c082f72d62 feat(s4): change a Micromate's settings by serial, audited, schedule-checked
s4_change_settings runs the shared change core with a read-back verdict
for its state (as asked / unchanged / differs).  It reads the active
setup's name (unstripped), the preferences, trigger and setup blocks and
the schedule; patches the setup through micromate.setup_fields; sends
THOR's setup push (0xDA 68 73 82 83 71 72, now enableable) once; reads
the setup back.  Refused before sending: a monitoring unit, a value the
unit does not allow (422), a stale preview (SettingsChanged, 409), and --
shown as 'blocked' in the preview -- the factory setup, a schedule that
loads another setup, an unreadable schedule, or a block SFM has not seen
(ChangeRefused, 409).  s4_settings carries the editable fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-11-03 21:43:03 -05:00

129 lines
3.8 KiB
Python

"""Typed device errors, each with the HTTP status the endpoints return."""
from __future__ import annotations
import micromate.protocol as _mm_proto
import minimateplus.protocol as _mmp_proto
class DeviceError(Exception):
http_status = 500
class BadRequest(DeviceError):
http_status = 422
class NotSupported(DeviceError):
http_status = 501
class WrongUnit(DeviceError):
http_status = 409
class UnitStateUnknown(DeviceError):
"""The unit reports a run state that has never been measured."""
http_status = 409
class AuditUnavailable(DeviceError):
"""The audit row could not be written, so nothing was sent."""
http_status = 503
class AuditIncomplete(DeviceError):
"""The unit was dealt with, but its audit row could not be finished. The
message says what happened to the unit first, then names the row.
`release_line` is True when the path would have hung up had the finish
worked (a ring, a link fault, a refusal): the failed write to the audit log
must not turn that into a held line, which would block a call-home."""
http_status = 500
def __init__(self, message: str, *, release_line: bool = False) -> None:
super().__init__(message)
self.release_line = release_line
class ChangeUnconfirmed(DeviceError):
"""A change failed after its frame went out, or may have gone out.
`sent` is "acknowledged" (the ack came back; the read-back failed) or
"unknown" (the ack was lost, or a call-home took its place). The status is
the one the cause would have had, set per instance, and the message names
the audit record. It never means "nothing was sent".
"""
http_status = 500
def __init__(self, message: str, *, audit_id: str, sent: str, http_status: int) -> None:
if sent not in ("acknowledged", "unknown"):
raise ValueError(f"sent must be 'acknowledged' or 'unknown', not {sent!r}")
super().__init__(message)
self.audit_id = audit_id
self.sent = sent
self.http_status = http_status
class UnitBusy(DeviceError):
http_status = 423
class ConnectFailed(DeviceError):
http_status = 503
class CallHomeInProgress(DeviceError):
http_status = 503
class NoAnswer(DeviceError):
http_status = 504
class MalformedReply(DeviceError):
http_status = 502
class EventNotFound(DeviceError):
http_status = 404
class SettingsChanged(DeviceError):
"""The unit's settings changed since the preview the request names (a keypad
edit, another program, or a preview of another unit). Nothing was sent."""
http_status = 409
class ChangeRefused(DeviceError):
"""A change SFM will not send to this unit, and why -- its factory setup, a
schedule that would undo it, a block SFM has not seen. Nothing was sent."""
http_status = 409
class StaleEvent(DeviceError):
"""The event at that key is not the one listed -- the unit was erased, or
its memory wrapped, since the list was read."""
http_status = 409
_TIMEOUTS = (_mm_proto.TimeoutError, _mmp_proto.TimeoutError)
_PROTOCOL = (_mm_proto.ProtocolError, _mmp_proto.ProtocolError)
def translate(exc: BaseException, *, connecting: bool) -> DeviceError:
"""Map a library exception to a typed error.
Order matters: each library's TimeoutError is a ProtocolError subclass, and
the builtin TimeoutError (a failed TCP connect) is an OSError.
"""
if isinstance(exc, DeviceError):
return exc
if isinstance(exc, _TIMEOUTS):
return NoAnswer(f"the unit did not answer: {exc}")
if isinstance(exc, _PROTOCOL):
return MalformedReply(f"bad reply from the unit: {exc}")
if isinstance(exc, OSError):
what = "could not connect" if connecting else "connection lost"
return ConnectFailed(f"{what}: {exc}")
return DeviceError(f"device error: {exc}")