fedimint_connectors/
error.rs1use fedimint_core::PeerId;
2use fedimint_core::util::SafeUrl;
3use fedimint_logging::LOG_CLIENT_NET_API;
4use thiserror::Error;
5use tracing::{trace, warn};
6
7#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum ServerError {
11 #[error("Response deserialization error: {0}")]
14 ResponseDeserialization(anyhow::Error),
15
16 #[error("Invalid peer id: {peer_id}")]
18 InvalidPeerId { peer_id: PeerId },
19
20 #[error("Invalid peer url: {url}")]
22 InvalidPeerUrl { url: SafeUrl, source: anyhow::Error },
23
24 #[error("Invalid endpoint")]
26 InvalidEndpoint(anyhow::Error),
27
28 #[error("Connection failed: {0}")]
30 Connection(anyhow::Error),
31
32 #[error("Transport error: {0}")]
34 Transport(anyhow::Error),
35
36 #[error("Invalid rpc id")]
42 InvalidRpcId(anyhow::Error),
43
44 #[error("Invalid request")]
47 InvalidRequest(anyhow::Error),
48
49 #[error("Invalid response: {0}")]
51 InvalidResponse(anyhow::Error),
52
53 #[error("Unspecified server error: {0}")]
55 ServerError(anyhow::Error),
56
57 #[error("Unspecified condition error: {0}")]
62 ConditionFailed(anyhow::Error),
63
64 #[error("Unspecified internal client error: {0}")]
69 InternalClientError(anyhow::Error),
70}
71
72impl ServerError {
73 pub fn is_unusual(&self) -> bool {
74 match self {
75 ServerError::ResponseDeserialization(_)
76 | ServerError::InvalidPeerId { .. }
77 | ServerError::InvalidPeerUrl { .. }
78 | ServerError::InvalidResponse(_)
79 | ServerError::InvalidRpcId(_)
80 | ServerError::InvalidRequest(_)
81 | ServerError::InternalClientError(_)
82 | ServerError::InvalidEndpoint(_)
83 | ServerError::ServerError(_) => true,
84 ServerError::Connection(_)
85 | ServerError::Transport(_)
86 | ServerError::ConditionFailed(_) => false,
87 }
88 }
89 pub fn report_if_unusual(&self, peer_id: PeerId, context: &str) {
95 let unusual = self.is_unusual();
96
97 trace!(target: LOG_CLIENT_NET_API, error = %self, %context, "ServerError");
98
99 if unusual {
100 warn!(target: LOG_CLIENT_NET_API, error = %self,%context, %peer_id, "Unusual ServerError");
101 }
102 }
103}