Skip to main content

muse_code_sdk/connection/
error.rs

1use std::io;
2
3use thiserror::Error;
4
5use crate::errors::{MspError, ProtocolError};
6
7/// Failure returned to one client request.
8#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum ConnectionError {
11    /// The connection driver is no longer running.
12    #[error("connection is closed")]
13    Closed,
14    /// The transport itself refused a read or write.
15    #[error(transparent)]
16    Transport(#[from] io::Error),
17    /// The peer sent a well-framed MSP error response.
18    #[error(transparent)]
19    Msp(#[from] MspError),
20    /// The peer broke the framing or correlation contract, or a request or
21    /// notification could not be built. See [`ProtocolError`].
22    #[error(transparent)]
23    Protocol(#[from] ProtocolError),
24}
25
26impl Clone for ConnectionError {
27    fn clone(&self) -> Self {
28        match self {
29            Self::Closed => Self::Closed,
30            // `io::Error` is not cloneable. The stable contract is its kind;
31            // retain the message as diagnostic context.
32            Self::Transport(error) => {
33                Self::Transport(io::Error::new(error.kind(), error.to_string()))
34            }
35            Self::Msp(error) => Self::Msp(error.clone()),
36            Self::Protocol(error) => Self::Protocol(error.clone()),
37        }
38    }
39}
40
41impl PartialEq for ConnectionError {
42    fn eq(&self, other: &Self) -> bool {
43        match (self, other) {
44            (Self::Closed, Self::Closed) => true,
45            (Self::Transport(left), Self::Transport(right)) => left.kind() == right.kind(),
46            (Self::Msp(left), Self::Msp(right)) => left == right,
47            (Self::Protocol(left), Self::Protocol(right)) => left == right,
48            _ => false,
49        }
50    }
51}