1use super::*;
5
6#[derive(Debug, Error, ErrorCode)]
7pub enum ClientError {
8 #[error(transparent)]
9 #[error_code(inherit)]
10 AddressValidation(#[from] IdentifierValidationError),
11 #[error("could not publish: {0}")]
15 PublishError(String),
16 #[error("storage error: {0}")]
20 Storage(#[from] StorageError),
21 #[error("API error: {0}")]
25 Api(#[from] xmtp_api::ApiError),
26 #[error("identity error: {0}")]
30 Identity(#[from] crate::identity::IdentityError),
31 #[error("TLS Codec error: {0}")]
35 TlsError(#[from] TlsCodecError),
36 #[error("key package verification: {0}")]
40 KeyPackageVerification(#[from] KeyPackageVerificationError),
41 #[error("Stream inconsistency error: {0}")]
45 StreamInconsistency(String),
46 #[error("Association error: {0}")]
50 Association(#[from] AssociationError),
51 #[error("signature validation error: {0}")]
55 SignatureValidation(#[from] SignatureError),
56 #[error(transparent)]
60 IdentityUpdate(#[from] IdentityUpdateError),
61 #[error(transparent)]
65 SignatureRequest(#[from] SignatureRequestError),
66 #[error(transparent)]
71 Group(Box<GroupError>),
72 #[error(transparent)]
76 LocalEvent(#[from] LocalEventError),
77 #[error(transparent)]
81 Db(#[from] xmtp_db::ConnectionError),
82 #[error("Failed to decode message")]
87 MessageDecodeFailed,
88 #[error(transparent)]
92 MlsStore(#[from] MlsStoreError),
93 #[error(transparent)]
97 EnrichMessage(#[from] EnrichMessageError),
98 #[error(transparent)]
102 Conversion(#[from] xmtp_proto::ConversionError),
103 #[error(transparent)]
104 #[error_code(inherit)]
105 Handle(#[from] crate::inbox_handles::HandleError),
106 #[error("Registration is not visible")]
111 RegistrationNotVisible,
112 #[error("client is closed")]
117 AlreadyClosed,
118 #[error("server configuration unavailable: {0}")]
125 ConfigurationUnavailable(#[source] Box<crate::server_configuration::ConfigurationFetchError>),
126 #[error("server configuration invalid: {0}")]
132 ConfigurationInvalid(#[from] xmtp_configuration::ServerConfigurationError),
133 #[error("this database is bound to backend {stored}, but {received} answered")]
139 BackendMismatch { stored: String, received: String },
140 #[error("client version {client} is below the {minimum} this backend requires")]
145 ClientVersionTooOld { client: String, minimum: String },
146 #[error(
151 "this backend requires authentication; required scopes: [{}]",
152 required_scopes.join(", ")
153 )]
154 AuthRequired { required_scopes: Vec<String> },
155 #[error(
160 "this backend does not accept smart contract wallet signatures on {chain}; accepted: [{}]",
161 accepted.join(", ")
162 )]
163 ChainNotAccepted {
164 chain: String,
165 accepted: Vec<String>,
166 },
167 #[error("invalid list options: {0}")]
173 InvalidListOptions(&'static str),
174 #[error("consent act: {0}")]
179 #[error_code(inherit)]
180 ConsentAct(#[from] ConsentActError),
181}
182
183impl ClientError {
184 #[deny(clippy::wildcard_enum_match_arm)]
185 pub fn db_needs_connection(&self) -> bool {
186 use crate::worker::NeedsDbReconnect;
187 match self {
188 Self::Storage(s) | Self::ConsentAct(ConsentActError::Storage(s)) => {
189 s.db_needs_connection()
190 }
191 Self::Db(c) => c.db_needs_connection(),
192 Self::Group(error) => error.needs_db_reconnect(),
193 Self::Identity(error) => error.needs_db_reconnect(),
194 Self::IdentityUpdate(error) => error.needs_db_reconnect(),
195 Self::MlsStore(error) => error.needs_db_reconnect(),
196 Self::ConfigurationUnavailable(error) => error.db_needs_connection(),
197 Self::ConsentAct(_)
198 | Self::AddressValidation(..)
199 | Self::PublishError(..)
200 | Self::Api(..)
201 | Self::TlsError(..)
202 | Self::KeyPackageVerification(..)
203 | Self::StreamInconsistency(..)
204 | Self::Association(..)
205 | Self::SignatureValidation(..)
206 | Self::SignatureRequest(..)
207 | Self::LocalEvent(..)
208 | Self::MessageDecodeFailed
209 | Self::EnrichMessage(..)
210 | Self::Conversion(..)
211 | Self::Handle(..)
212 | Self::RegistrationNotVisible
213 | Self::AlreadyClosed
214 | Self::ConfigurationInvalid(..)
215 | Self::BackendMismatch { .. }
216 | Self::ClientVersionTooOld { .. }
217 | Self::AuthRequired { .. }
218 | Self::ChainNotAccepted { .. }
219 | Self::InvalidListOptions(..) => false,
220 }
221 }
222}
223
224impl From<NotFound> for ClientError {
225 fn from(value: NotFound) -> Self {
226 ClientError::Storage(StorageError::NotFound(value))
227 }
228}
229
230impl From<GroupError> for ClientError {
231 fn from(err: GroupError) -> ClientError {
232 ClientError::Group(Box::new(err))
233 }
234}
235
236impl From<BlockedConnection> for ClientError {
237 fn from(blocked_connection: BlockedConnection) -> Self {
238 match blocked_connection {
239 BlockedConnection::BackendMismatch { stored, received } => {
240 ClientError::BackendMismatch { stored, received }
241 }
242 BlockedConnection::ClientVersionTooOld { client, minimum } => {
243 ClientError::ClientVersionTooOld { client, minimum }
244 }
245 }
246 }
247}
248
249impl From<ServerConfigError> for ClientError {
250 fn from(error: ServerConfigError) -> Self {
251 match error {
252 ServerConfigError::Unavailable(error) => ClientError::ConfigurationUnavailable(error),
253 ServerConfigError::Invalid(error) => ClientError::ConfigurationInvalid(error),
254 ServerConfigError::Blocked(blocked_connection) => blocked_connection.into(),
255 ServerConfigError::Closed => ClientError::AlreadyClosed,
256 }
257 }
258}
259
260impl xmtp_common::RetryableError for ClientError {
261 fn is_retryable(&self) -> bool {
262 match self {
263 ClientError::Group(group_error) => retryable!(group_error),
264 ClientError::IdentityUpdate(error) => retryable!(error),
265 ClientError::Api(api_error) => retryable!(api_error),
266 ClientError::Storage(storage_error) => retryable!(storage_error),
267 ClientError::Db(db) => retryable!(db),
268 ClientError::SignatureValidation(e) => retryable!(e),
272 ClientError::ConfigurationUnavailable(e) => retryable!(e),
276 _ => false,
277 }
278 }
279
280 fn rate_limit_delay(&self) -> Option<xmtp_common::time::Duration> {
281 match self {
282 ClientError::Group(group_error) => group_error.rate_limit_delay(),
283 ClientError::IdentityUpdate(error) => error.rate_limit_delay(),
284 ClientError::Api(api_error) => api_error.rate_limit_delay(),
285 ClientError::SignatureValidation(e) => e.rate_limit_delay(),
286 ClientError::ConfigurationUnavailable(e) => e.rate_limit_delay(),
287 _ => None,
288 }
289 }
290}
291
292pub fn stream_failure_details(
297 error: &(dyn std::error::Error + 'static),
298) -> Option<crate::subscriptions::stream_failure::StreamFailureDetails> {
299 crate::subscriptions::stream_failure::stream_failure_details_opening(error, |error| {
300 match error.downcast_ref::<ClientError>() {
301 Some(ClientError::Group(error)) => Some(error.as_ref()),
302 _ => None,
303 }
304 })
305}