Skip to main content

isothrum_client/
error.rs

1//! Why a client call failed.
2
3use isothrum_runner::store::OperationId;
4use isothrum_runner::{ErrorCode, ErrorReport};
5
6/// Why a client call failed.
7#[derive(Debug, thiserror::Error)]
8#[non_exhaustive]
9pub enum ClientError {
10    /// No service answered for the home, and none could be started.
11    #[error("no runner service for this home: {0}")]
12    Unavailable(String),
13    /// A frame the other side can't read, or a protocol the service doesn't speak.
14    #[error("protocol: {0}")]
15    Protocol(String),
16    /// The service refused the request: the runner's report of why (boxed: it is large, and
17    /// rare).
18    #[error("{0}")]
19    Refused(Box<ErrorReport>),
20    /// The operation failed (its `error` event's report), or was cancelled.
21    #[error("{0}")]
22    Operation(Box<ErrorReport>),
23    /// The home's service is another release and has operations queued or running: it was left
24    /// running ([`Client::connect_exact`](crate::Client::connect_exact)).
25    #[error("the runner service (Isothrum {version}, pid {pid}) has {} operation(s) queued or running: it was left running", operations.len())]
26    Busy {
27        /// The service's release.
28        version: String,
29        /// Its process id.
30        pid: u32,
31        /// Its operations queued or running, oldest first.
32        operations: Vec<OperationId>,
33    },
34    /// The service started is another release than this client.
35    #[error("the runner service is Isothrum {service}, this client {client}")]
36    Version {
37        /// This client's release ([`VERSION`](crate::VERSION)).
38        client: String,
39        /// The service's.
40        service: String,
41    },
42    /// Reading or writing the connection failed.
43    #[error(transparent)]
44    Io(#[from] std::io::Error),
45    /// The request was sent, but the connection ended before the service answered it: whether
46    /// the service acted on it is unknown (an operation may have been queued).
47    #[error("the runner service's connection ended before it answered")]
48    Unanswered,
49}
50
51impl ClientError {
52    /// Whether it says the connection is gone (the service was killed or restarted, the
53    /// connection broke, none answers), rather than an answer of the service's: a call worth one
54    /// more try on a new connection ([`Pool::call`](crate::Pool::call)). An operation sent but
55    /// unanswered ([`ClientError::Unanswered`]) may have been queued: it is followed again
56    /// ([`Pool::follow`](crate::Pool::follow)), not sent again.
57    #[must_use]
58    pub fn is_broken(&self) -> bool {
59        matches!(
60            self,
61            ClientError::Io(_) | ClientError::Protocol(_) | ClientError::Unavailable(_) | ClientError::Unanswered
62        )
63    }
64
65    /// Its code, as an `error` event gives it.
66    #[must_use]
67    pub fn code(&self) -> ErrorCode {
68        match self {
69            ClientError::Refused(report) | ClientError::Operation(report) => report.code,
70            ClientError::Busy { .. } => ErrorCode::Locked,
71            ClientError::Version { .. } => ErrorCode::Unsupported,
72            ClientError::Unavailable(_) | ClientError::Protocol(_) | ClientError::Io(_) | ClientError::Unanswered => ErrorCode::Io,
73        }
74    }
75
76    /// The runner's report of the failure, when the service or the operation gave one.
77    #[must_use]
78    pub fn report(&self) -> Option<&ErrorReport> {
79        match self {
80            ClientError::Refused(report) | ClientError::Operation(report) => Some(report),
81            _ => None,
82        }
83    }
84
85    /// The connection ended under a call.
86    pub(crate) fn closed() -> ClientError {
87        ClientError::Unavailable("the service closed the connection".to_owned())
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn each_failure_has_the_code_an_error_event_would() {
97        let refused = ClientError::Refused(Box::new(ErrorReport::new(ErrorCode::NotRunning, "x")));
98        assert_eq!(refused.code(), ErrorCode::NotRunning);
99        let busy = ClientError::Busy {
100            version: "2.1.7".into(),
101            pid: 1,
102            operations: Vec::new(),
103        };
104        assert_eq!(busy.code(), ErrorCode::Locked);
105        let version = ClientError::Version {
106            client: "3.0.0".into(),
107            service: "2.1.7".into(),
108        };
109        assert_eq!(version.code(), ErrorCode::Unsupported);
110        for io in [
111            ClientError::Protocol("x".into()),
112            ClientError::Unavailable("x".into()),
113            ClientError::closed(),
114            ClientError::Unanswered,
115        ] {
116            assert_eq!(io.code(), ErrorCode::Io, "{io}");
117        }
118        assert!(matches!(ClientError::closed(), ClientError::Unavailable(m) if m == "the service closed the connection"));
119    }
120}