The final fix wave's AuditIncomplete landed in _exchange's `except DeviceError`, which keeps the line -- so on the nine ring / link-fault paths (and a refusal) a failed audit write turned a hang-up into a held Series-4 line, blocking a THOR call-home for up to the 5-minute idle hang-up and making "SFM hung up" false. AuditIncomplete now carries release_line, set by every finish whose path hangs up anyway, and _exchange honours it. Also pins the Series-3 change path's retry=False again (the rewritten lost-ack test no longer could). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
117 lines
3.4 KiB
Python
117 lines
3.4 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 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}")
|