xmtp_proto/traits/
error.rs1use std::fmt::Display;
2
3use crate::{ApiEndpoint, ProtoError};
4use thiserror::Error;
5use xmtp_common::{BoxDynError, ErrorCode, RetryableError, retryable};
6
7#[derive(Clone, Copy, Debug, Error, ErrorCode)]
10pub enum AuthError {
11 #[error("credential rejected")]
13 CredentialRejected { retryable: bool },
14 #[error("auth callback failed")]
16 CallbackFailed { retryable: bool },
17 #[error("auth attempts exhausted")]
19 Exhausted,
20 #[error("auth attempts exhausted")]
23 #[error_code("AuthError::Exhausted")]
24 ExhaustedAfterAttempt,
25 #[error("auth credential missing")]
27 MissingCredential,
28}
29
30impl RetryableError for AuthError {
31 fn is_retryable(&self) -> bool {
32 match self {
33 Self::CredentialRejected { retryable } | Self::CallbackFailed { retryable } => {
34 *retryable
35 }
36 Self::Exhausted | Self::ExhaustedAfterAttempt | Self::MissingCredential => false,
37 }
38 }
39}
40
41impl AuthError {
42 pub fn is_locked_out(&self) -> bool {
47 matches!(self, Self::Exhausted | Self::ExhaustedAfterAttempt)
48 }
49}
50
51impl ApiClientError {
52 pub fn is_locked_out(&self) -> bool {
56 matches!(self, Self::Auth(auth) if auth.is_locked_out())
57 }
58}
59
60#[derive(Debug, Error, ErrorCode)]
61#[non_exhaustive]
62pub enum ApiClientError {
63 #[error(transparent)]
64 #[error_code(inherit)]
65 Auth(#[from] AuthError),
66 #[error("api client at endpoint \"{}\" has error {}", endpoint, source)]
68 ClientWithEndpoint {
69 endpoint: String,
70 source: NetworkError,
72 },
73 #[error("client errored {}", source)]
75 Client { source: NetworkError },
76 #[error(transparent)]
78 Http(#[from] http::Error),
79 #[error(transparent)]
81 Body(#[from] BodyError),
82 #[error(transparent)]
84 DecodeError(#[from] prost::DecodeError),
85 #[error(transparent)]
87 Conversion(#[from] crate::ConversionError),
88 #[error(transparent)]
90 ProtoError(#[from] ProtoError),
91 #[error(transparent)]
93 InvalidUri(#[from] http::uri::InvalidUri),
94 #[error(transparent)]
96 Expired(#[from] xmtp_common::time::Expired),
97 #[error("{0}")]
99 Other(Box<dyn RetryableError>),
100 #[error("{0}")]
102 OtherUnretryable(BoxDynError),
103 #[error("Writes are disabled on this client.")]
105 WritesDisabled,
106}
107
108#[derive(Debug)]
112pub struct NetworkError {
113 source: Box<dyn RetryableError>,
114}
115
116impl std::error::Error for NetworkError {
117 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
118 Some(self.source.as_ref())
119 }
120}
121
122impl Display for NetworkError {
123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124 write!(f, "{}", self.source)
125 }
126}
127
128impl RetryableError for NetworkError {
129 fn is_retryable(&self) -> bool {
130 self.source.is_retryable()
131 }
132}
133
134impl NetworkError {
135 pub fn new(e: impl RetryableError + 'static) -> Self {
136 NetworkError {
137 source: Box::new(e),
138 }
139 }
140}
141
142impl ApiClientError {
143 pub fn new(endpoint: ApiEndpoint, source: impl RetryableError + 'static) -> Self {
144 Self::ClientWithEndpoint {
145 endpoint: endpoint.to_string(),
146 source: NetworkError::new(source),
147 }
148 }
149
150 pub fn endpoint(self, endpoint: impl ToString) -> Self {
152 match self {
153 Self::Client { source } => Self::ClientWithEndpoint {
154 source,
155 endpoint: endpoint.to_string(),
156 },
157 v => v,
158 }
159 }
160
161 pub fn client(client: impl RetryableError + 'static) -> Self {
162 Self::Client {
163 source: NetworkError::new(client),
164 }
165 }
166
167 pub fn network_error(&self) -> Option<&NetworkError> {
170 use ApiClientError::*;
171 match self {
172 ClientWithEndpoint { source, .. } | Client { source, .. } => Some(source),
173 _ => None,
174 }
175 }
176}
177
178impl ApiClientError {
179 pub fn other<R: RetryableError + 'static>(e: R) -> Self {
180 ApiClientError::Other(Box::new(e))
181 }
182}
183
184impl RetryableError for ApiClientError {
185 fn is_retryable(&self) -> bool {
186 use ApiClientError::*;
187 match self {
188 Client { source } => retryable!(*source),
189 ClientWithEndpoint { source, .. } => retryable!(source),
190 Auth(e) => retryable!(e),
191 Body(e) => retryable!(e),
192 Http(_) => false,
193 DecodeError(_) => false,
194 Conversion(_) => false,
195 ProtoError(_) => false,
196 InvalidUri(_) => false,
197 Expired(_) => true,
198 Other(r) => retryable!(r),
199 OtherUnretryable(_) => false,
200 WritesDisabled => false,
201 }
202 }
203}
204
205impl From<std::convert::Infallible> for ApiClientError {
207 fn from(_v: std::convert::Infallible) -> ApiClientError {
208 unreachable!("Infallible errors can never occur")
209 }
210}
211
212#[derive(Debug, Error)]
213pub enum BodyError {
214 #[error(transparent)]
215 UninitializedField(#[from] derive_builder::UninitializedFieldError),
216 #[error(transparent)]
217 Conversion(#[from] crate::ConversionError),
218}
219
220impl RetryableError for BodyError {
221 fn is_retryable(&self) -> bool {
222 false
223 }
224}
225
226pub fn grpc_status<'a>(
228 mut error: &'a (dyn std::error::Error + 'static),
229) -> Option<&'a tonic::Status> {
230 loop {
231 if let Some(status) = error.downcast_ref::<tonic::Status>() {
232 return Some(status);
233 }
234 error = match error.downcast_ref::<ApiClientError>() {
235 Some(ApiClientError::Other(inner)) => inner.as_ref(),
236 Some(ApiClientError::OtherUnretryable(inner)) => inner.as_ref(),
237 _ => error.source()?,
238 };
239 }
240}