Skip to main content

microsandbox_control_client/
error.rs

1//! Checked-operation errors retaining the actual peer response.
2
3use microsandbox_protocol::{control::ControlError, wire::WireError};
4use microsandbox_protocol_client::{ClientError, Delivery, Message};
5use thiserror::Error;
6
7//--------------------------------------------------------------------------------------------------
8// Types
9//--------------------------------------------------------------------------------------------------
10
11/// Operation failure distinct from generic routing and application observations.
12#[derive(Debug, Error)]
13pub enum ControlClientError {
14    /// Transport or local admission failure with delivery certainty.
15    #[error(transparent)]
16    Client(#[from] ClientError),
17    /// Invalid locally prepared payload.
18    #[error(transparent)]
19    Wire(#[from] WireError),
20    /// Valid structured peer error, retaining original frame and unknown fields.
21    #[error("control operation rejected by peer")]
22    Peer {
23        /// Exact decoded public error, including unknown future codes.
24        error: ControlError,
25        /// Original framed response.
26        response: Box<Message>,
27    },
28    /// A response did not match the checked operation's contract.
29    #[error("invalid control operation response")]
30    InvalidResponse {
31        /// Original response remains inspectable without re-encoding.
32        response: Box<Message>,
33    },
34    /// Raw/encoded framed access is unavailable in legacy JSON mode.
35    #[error("operation requires framed control (delivery: NotSent)")]
36    UnsupportedMode,
37    /// Verified runtime continuity failed, or fresh discovery changed format.
38    #[error("runtime session changed before request admission (delivery: NotSent)")]
39    RuntimeChanged,
40    /// Legacy operation rejection. Diagnostics are not structured retry codes;
41    /// secret-batch progress is unknown and earlier changes may remain applied.
42    #[error("legacy control operation rejected by peer; batch progress unknown")]
43    LegacyRemote {
44        /// Original response, including the peer's diagnostic and extensions.
45        reply: Box<crate::JsonReply>,
46    },
47    /// A syntactically valid JSON reply violated a checked operation's shape.
48    #[error("invalid legacy control operation response")]
49    InvalidJsonResponse {
50        /// Original response remains inspectable.
51        reply: Box<crate::JsonReply>,
52    },
53}
54
55/// Result from an optional checked control operation.
56pub type ControlClientResult<T> = Result<T, ControlClientError>;
57
58//--------------------------------------------------------------------------------------------------
59// Methods
60//--------------------------------------------------------------------------------------------------
61
62impl ControlClientError {
63    /// Request admission certainty. A peer's structured effect field can carry
64    /// additional mutation information; it never authorizes automatic replay.
65    pub fn delivery(&self) -> Delivery {
66        match self {
67            Self::Client(error) => error.delivery,
68            Self::Wire(_) | Self::UnsupportedMode | Self::RuntimeChanged => Delivery::NotSent,
69            Self::Peer { .. }
70            | Self::InvalidResponse { .. }
71            | Self::LegacyRemote { .. }
72            | Self::InvalidJsonResponse { .. } => Delivery::Unknown,
73        }
74    }
75}