Final fix wave for phase 3 (start/stop monitoring), per the whole-phase review. - I1 + M2: every failure after the send has started is a ChangeUnconfirmed (audit_id, sent = "acknowledged" | "unknown", the cause's HTTP status) whose message says the frame was sent and names the audit record -- the ring in place of the ack, the four lost-ack follow-ups, and the three read-back paths. Series4Session releases the line on it. Every audit finish goes through one helper; a finish that fails is AuditIncomplete (500) and still says what happened to the unit. The line is kept. - I2: device_commands gains `sent` (1 acknowledged, 0 nothing sent, NULL unknown) and `states` (before / seen / after), with an idempotent ADD COLUMN in _migrate for a table created earlier on this branch. - M1: a RADIO RING already buffered is caught before 0x96/0x97 goes out (pre_send = read_unsolicited, one 10 ms transport read). That is a CallHomeInProgress with sent=0, not ChangeUnconfirmed. - M3: Series 3 reads the raw 0x1C flag (new MonitorStatus.state_flag); a byte other than 0x00/0x10, or a short section, is unknown(...) and refused. - M8: even_if_already on a unit already in the state notes that the read-back proves nothing. - M10: a scan pins `.protocol.execute(` to sfm/device_access/s4.py; both scans fail closed on a file that does not parse. - M11: docs corrected (CLAUDE.md phase-2 sentence, phase-3 bullet, REST list; micromate/protocol.py scope and the 0x49 read_state docstring; s4.py; route docstrings). "No command has ever been originated" is NOT retracted. - M12: GET /db/device_commands normalises the serial. - Minors: unused imports, S3 dry-run frames and 120 s timeout pinned, test rename, serial-cache and short-0x49 tests, _RUN_STATES comment. Schema: two NULLable columns on the branch-new device_commands table, added by _migrate on open; no backfill. No codec change, TOOL_VERSION unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
109 lines
3.0 KiB
Python
109 lines
3.0 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."""
|
|
http_status = 500
|
|
|
|
|
|
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 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}")
|