commonware_sync/
error.rs

1//! Error types for the sync example.
2
3use commonware_storage::adb::any::sync::Error as SyncError;
4use std::net::SocketAddr;
5use thiserror::Error;
6
7/// Errors that can occur in the sync example.
8#[derive(Debug, Error)]
9pub enum Error {
10    /// Failed to establish connection to server
11    #[error("failed to connect to server at {addr}: {source}")]
12    ConnectionFailed {
13        addr: SocketAddr,
14        #[source]
15        source: commonware_runtime::Error,
16    },
17
18    /// Network runtime error
19    #[error("runtime network error")]
20    RuntimeNetwork(#[from] commonware_runtime::Error),
21
22    /// Stream error during communication
23    #[error("stream error")]
24    Stream(#[from] commonware_stream::Error),
25
26    /// Failed to encode message for transmission
27    #[error("failed to encode message")]
28    Encode(#[from] commonware_codec::Error),
29
30    /// Failed to decode received message
31    #[error("failed to decode message")]
32    Decode(#[source] commonware_codec::Error),
33
34    /// Received unexpected response type for a request
35    #[error("unexpected response type for request {request_id}")]
36    UnexpectedResponse { request_id: u64 },
37
38    /// Server returned an error response
39    #[error("server error (code: {code:?}): {message}")]
40    ServerError {
41        code: crate::ErrorCode,
42        message: String,
43    },
44
45    /// Invalid request parameters
46    #[error("invalid request: {0}")]
47    InvalidRequest(String),
48
49    /// Database operation failed
50    #[error("database operation failed")]
51    Database(#[from] commonware_storage::adb::Error),
52
53    /// Request channel to I/O task closed unexpectedly
54    #[error("request channel closed - I/O task may have terminated")]
55    RequestChannelClosed,
56
57    /// Response channel closed before receiving response
58    #[error("response channel closed for request {request_id}")]
59    ResponseChannelClosed { request_id: u64 },
60
61    /// Target update channel error
62    #[error("target update channel error: {reason}")]
63    TargetUpdateChannel { reason: String },
64
65    /// Configuration error
66    #[error("invalid configuration: {0}")]
67    InvalidConfig(String),
68
69    /// Sync operation failed
70    #[error("sync failed")]
71    Sync(#[from] SyncError),
72}
73
74impl Error {
75    /// Convert this error to a protocol error code for transmission over the network.
76    pub fn to_error_code(&self) -> crate::ErrorCode {
77        match self {
78            Error::InvalidRequest(_) => crate::ErrorCode::InvalidRequest,
79            Error::Database(_) => crate::ErrorCode::DatabaseError,
80            Error::RuntimeNetwork(_) | Error::Stream(_) | Error::ConnectionFailed { .. } => {
81                crate::ErrorCode::NetworkError
82            }
83            Error::RequestChannelClosed | Error::ResponseChannelClosed { .. } => {
84                crate::ErrorCode::InternalError
85            }
86            _ => crate::ErrorCode::InternalError,
87        }
88    }
89}
90
91// Convert our error type to sync error for trait compatibility
92impl From<Error> for SyncError {
93    fn from(err: Error) -> Self {
94        SyncError::Resolver(Box::new(err))
95    }
96}