1use std::{io::Error as IoError, sync::Arc, time::Duration};
18
19use as_variant::as_variant;
20use http::StatusCode;
21#[cfg(feature = "qrcode")]
22use matrix_sdk_base::crypto::ScanError;
23#[cfg(feature = "e2e-encryption")]
24use matrix_sdk_base::crypto::{
25 CryptoStoreError, DecryptorError, KeyExportError, MegolmError, OlmError,
26};
27use matrix_sdk_base::{
28 event_cache::store::EventCacheStoreError, Error as SdkBaseError, QueueWedgeError, RoomState,
29 StoreError,
30};
31use reqwest::Error as ReqwestError;
32use ruma::{
33 api::{
34 client::{
35 error::{ErrorBody, ErrorKind, RetryAfter},
36 uiaa::{UiaaInfo, UiaaResponse},
37 },
38 error::{FromHttpResponseError, IntoHttpError},
39 },
40 events::tag::InvalidUserTagName,
41 push::{InsertPushRuleError, RemovePushRuleError},
42 IdParseError,
43};
44use serde_json::Error as JsonError;
45use thiserror::Error;
46use url::ParseError as UrlParseError;
47
48use crate::{event_cache::EventCacheError, media::MediaError, store_locks::LockStoreError};
49
50pub type Result<T, E = Error> = std::result::Result<T, E>;
52
53pub type HttpResult<T> = std::result::Result<T, HttpError>;
55
56#[derive(Error, Debug)]
59pub enum RumaApiError {
60 #[error(transparent)]
62 ClientApi(ruma::api::client::Error),
63
64 #[error("User-Interactive Authentication required.")]
71 Uiaa(UiaaInfo),
72
73 #[error(transparent)]
75 Other(ruma::api::error::MatrixError),
76}
77
78impl RumaApiError {
79 pub fn as_client_api_error(&self) -> Option<&ruma::api::client::Error> {
83 as_variant!(self, Self::ClientApi)
84 }
85}
86
87#[derive(Error, Debug)]
90pub enum HttpError {
91 #[error(transparent)]
93 Reqwest(#[from] ReqwestError),
94
95 #[error("the queried endpoint is not meant for clients")]
97 NotClientRequest,
98
99 #[error(transparent)]
101 Api(#[from] FromHttpResponseError<RumaApiError>),
102
103 #[error(transparent)]
106 IntoHttp(IntoHttpError),
107
108 #[error(transparent)]
110 RefreshToken(RefreshTokenError),
111}
112
113#[rustfmt::skip] impl HttpError {
115 pub fn as_ruma_api_error(&self) -> Option<&RumaApiError> {
121 as_variant!(self, Self::Api(FromHttpResponseError::Server(e)) => e)
122 }
123
124 pub fn as_client_api_error(&self) -> Option<&ruma::api::client::Error> {
127 self.as_ruma_api_error().and_then(RumaApiError::as_client_api_error)
128 }
129}
130
131impl HttpError {
133 pub fn client_api_error_kind(&self) -> Option<&ErrorKind> {
136 self.as_client_api_error()
137 .and_then(|e| as_variant!(&e.body, ErrorBody::Standard { kind, .. } => kind))
138 }
139
140 pub fn as_uiaa_response(&self) -> Option<&UiaaInfo> {
152 self.as_ruma_api_error().and_then(as_variant!(RumaApiError::Uiaa))
153 }
154
155 pub(crate) fn retry_kind(&self) -> RetryKind {
158 match self {
159 HttpError::Reqwest(_) => RetryKind::NetworkFailure,
162
163 HttpError::Api(FromHttpResponseError::Server(api_error)) => {
164 RetryKind::from_api_error(api_error)
165 }
166 _ => RetryKind::Permanent,
167 }
168 }
169}
170
171pub(crate) enum RetryKind {
174 NetworkFailure,
176
177 Transient {
181 #[cfg_attr(target_arch = "wasm32", allow(dead_code))]
183 retry_after: Option<Duration>,
184 },
185
186 Permanent,
189}
190
191impl RetryKind {
192 fn from_api_error(api_error: &RumaApiError) -> Self {
199 use ruma::api::client::Error;
200
201 match api_error {
202 RumaApiError::ClientApi(client_error) => {
203 let Error { status_code, body, .. } = client_error;
204
205 match body {
206 ErrorBody::Standard { kind, .. } => match kind {
207 ErrorKind::LimitExceeded { retry_after } => {
208 RetryKind::from_retry_after(retry_after.as_ref())
209 }
210 ErrorKind::Unrecognized => RetryKind::Permanent,
211 _ => RetryKind::from_status_code(*status_code),
212 },
213 _ => RetryKind::from_status_code(*status_code),
214 }
215 }
216 RumaApiError::Other(e) => RetryKind::from_status_code(e.status_code),
217 RumaApiError::Uiaa(_) => RetryKind::Permanent,
218 }
219 }
220
221 fn from_retry_after(retry_after: Option<&RetryAfter>) -> Self {
227 let retry_after = retry_after
228 .and_then(|retry_after| match retry_after {
229 RetryAfter::Delay(d) => Some(d),
230 RetryAfter::DateTime(_) => None,
231 })
232 .copied();
233
234 Self::Transient { retry_after }
235 }
236
237 fn from_status_code(status_code: StatusCode) -> Self {
244 if status_code == StatusCode::TOO_MANY_REQUESTS || status_code.is_server_error() {
248 RetryKind::Transient { retry_after: None }
249 } else {
250 RetryKind::Permanent
251 }
252 }
253}
254
255#[derive(Error, Debug)]
257#[non_exhaustive]
258pub enum Error {
259 #[error(transparent)]
261 Http(#[from] HttpError),
262
263 #[error("the queried endpoint requires authentication but was called before logging in")]
266 AuthenticationRequired,
267
268 #[error("Local cache doesn't contain all necessary data to perform the action.")]
270 InsufficientData,
271
272 #[cfg(feature = "e2e-encryption")]
275 #[error("The olm machine has already been initialized")]
276 BadCryptoStoreState,
277
278 #[cfg(feature = "e2e-encryption")]
280 #[error("The olm machine isn't yet available")]
281 NoOlmMachine,
282
283 #[error(transparent)]
285 SerdeJson(#[from] JsonError),
286
287 #[error(transparent)]
289 Io(#[from] IoError),
290
291 #[cfg(feature = "e2e-encryption")]
293 #[error(transparent)]
294 CryptoStoreError(#[from] CryptoStoreError),
295
296 #[error(transparent)]
298 CrossProcessLockError(#[from] LockStoreError),
299
300 #[cfg(feature = "e2e-encryption")]
302 #[error(transparent)]
303 OlmError(#[from] OlmError),
304
305 #[cfg(feature = "e2e-encryption")]
307 #[error(transparent)]
308 MegolmError(#[from] MegolmError),
309
310 #[cfg(feature = "e2e-encryption")]
312 #[error(transparent)]
313 DecryptorError(#[from] DecryptorError),
314
315 #[error(transparent)]
317 StateStore(#[from] StoreError),
318
319 #[error(transparent)]
321 EventCacheStore(#[from] EventCacheStoreError),
322
323 #[error(transparent)]
325 Identifier(#[from] IdParseError),
326
327 #[error(transparent)]
329 Url(#[from] UrlParseError),
330
331 #[cfg(feature = "qrcode")]
333 #[error(transparent)]
334 QrCodeScanError(#[from] ScanError),
335
336 #[error(transparent)]
338 UserTagName(#[from] InvalidUserTagName),
339
340 #[error(transparent)]
342 SlidingSync(#[from] crate::sliding_sync::Error),
343
344 #[error("wrong room state: {0}")]
348 WrongRoomState(WrongRoomState),
349
350 #[error("session callbacks have been set multiple times")]
352 MultipleSessionCallbacks,
353
354 #[error(transparent)]
356 OAuth(#[from] crate::authentication::oauth::OAuthError),
357
358 #[error("a concurrent request failed; see logs for details")]
360 ConcurrentRequestFailed,
361
362 #[error("unknown error: {0}")]
367 UnknownError(Box<dyn std::error::Error + Send + Sync>),
368
369 #[error(transparent)]
371 EventCache(#[from] EventCacheError),
372
373 #[error(transparent)]
375 SendQueueWedgeError(#[from] QueueWedgeError),
376
377 #[error("backups are not enabled")]
379 BackupNotEnabled,
380
381 #[error(transparent)]
383 Media(#[from] MediaError),
384}
385
386#[rustfmt::skip] impl Error {
388 pub fn as_ruma_api_error(&self) -> Option<&RumaApiError> {
394 as_variant!(self, Self::Http).and_then(|e| e.as_ruma_api_error())
395 }
396
397 pub fn as_client_api_error(&self) -> Option<&ruma::api::client::Error> {
400 self.as_ruma_api_error().and_then(RumaApiError::as_client_api_error)
401 }
402
403 pub fn client_api_error_kind(&self) -> Option<&ErrorKind> {
406 self.as_client_api_error().and_then(|e| {
407 as_variant!(&e.body, ErrorBody::Standard { kind, .. } => kind)
408 })
409 }
410
411 pub fn as_uiaa_response(&self) -> Option<&UiaaInfo> {
423 self.as_ruma_api_error().and_then(as_variant!(RumaApiError::Uiaa))
424 }
425}
426
427#[cfg(feature = "e2e-encryption")]
429#[derive(Error, Debug)]
430#[allow(dead_code)]
432pub enum RoomKeyImportError {
433 #[error(transparent)]
435 SerdeJson(#[from] JsonError),
436
437 #[error("The crypto store hasn't been yet opened, can't import yet.")]
440 StoreClosed,
441
442 #[error(transparent)]
444 Io(#[from] IoError),
445
446 #[error(transparent)]
448 CryptoStore(#[from] CryptoStoreError),
449
450 #[error(transparent)]
452 Export(#[from] KeyExportError),
453}
454
455impl From<FromHttpResponseError<ruma::api::client::Error>> for HttpError {
456 fn from(err: FromHttpResponseError<ruma::api::client::Error>) -> Self {
457 Self::Api(err.map(RumaApiError::ClientApi))
458 }
459}
460
461impl From<FromHttpResponseError<UiaaResponse>> for HttpError {
462 fn from(err: FromHttpResponseError<UiaaResponse>) -> Self {
463 Self::Api(err.map(|e| match e {
464 UiaaResponse::AuthResponse(i) => RumaApiError::Uiaa(i),
465 UiaaResponse::MatrixError(e) => RumaApiError::ClientApi(e),
466 }))
467 }
468}
469
470impl From<FromHttpResponseError<ruma::api::error::MatrixError>> for HttpError {
471 fn from(err: FromHttpResponseError<ruma::api::error::MatrixError>) -> Self {
472 Self::Api(err.map(RumaApiError::Other))
473 }
474}
475
476impl From<SdkBaseError> for Error {
477 fn from(e: SdkBaseError) -> Self {
478 match e {
479 SdkBaseError::StateStore(e) => Self::StateStore(e),
480 #[cfg(feature = "e2e-encryption")]
481 SdkBaseError::CryptoStore(e) => Self::CryptoStoreError(e),
482 #[cfg(feature = "e2e-encryption")]
483 SdkBaseError::BadCryptoStoreState => Self::BadCryptoStoreState,
484 #[cfg(feature = "e2e-encryption")]
485 SdkBaseError::OlmError(e) => Self::OlmError(e),
486 #[cfg(feature = "eyre")]
487 _ => Self::UnknownError(eyre::eyre!(e).into()),
488 #[cfg(all(not(feature = "eyre"), feature = "anyhow"))]
489 _ => Self::UnknownError(anyhow::anyhow!(e).into()),
490 #[cfg(all(not(feature = "eyre"), not(feature = "anyhow")))]
491 _ => {
492 let e: Box<dyn std::error::Error + Sync + Send> = format!("{e:?}").into();
493 Self::UnknownError(e)
494 }
495 }
496 }
497}
498
499impl From<ReqwestError> for Error {
500 fn from(e: ReqwestError) -> Self {
501 Error::Http(HttpError::Reqwest(e))
502 }
503}
504
505#[derive(Debug, Error)]
507pub enum BeaconError {
508 #[error("Network error: {0}")]
510 Network(#[from] HttpError),
511
512 #[error("Existing beacon information not found.")]
514 NotFound,
515
516 #[error("Beacon event is redacted and cannot be processed.")]
518 Redacted,
519
520 #[error("Must join the room to access beacon information.")]
522 Stripped,
523
524 #[error("Deserialization error: {0}")]
526 Deserialization(#[from] serde_json::Error),
527
528 #[error("The beacon event has expired.")]
530 NotLive,
531
532 #[error("Other error: {0}")]
534 Other(Box<Error>),
535}
536
537impl From<Error> for BeaconError {
538 fn from(err: Error) -> Self {
539 BeaconError::Other(Box::new(err))
540 }
541}
542
543#[derive(Debug, Error, Clone)]
551pub enum RefreshTokenError {
552 #[error("missing refresh token")]
554 RefreshTokenRequired,
555
556 #[error(transparent)]
558 MatrixAuth(Arc<HttpError>),
559
560 #[error(transparent)]
562 OAuth(#[from] Arc<crate::authentication::oauth::OAuthError>),
563}
564
565#[derive(Debug, Error, Clone, PartialEq)]
567pub enum NotificationSettingsError {
568 #[error("Invalid parameter `{0}`")]
570 InvalidParameter(String),
571 #[error("Unable to add push rule")]
573 UnableToAddPushRule,
574 #[error("Unable to remove push rule")]
576 UnableToRemovePushRule,
577 #[error("Unable to update push rule")]
579 UnableToUpdatePushRule,
580 #[error("Rule `{0}` not found")]
582 RuleNotFound(String),
583 #[error("Unable to save push rules")]
585 UnableToSavePushRules,
586}
587
588impl From<InsertPushRuleError> for NotificationSettingsError {
589 fn from(_: InsertPushRuleError) -> Self {
590 Self::UnableToAddPushRule
591 }
592}
593
594impl From<RemovePushRuleError> for NotificationSettingsError {
595 fn from(_: RemovePushRuleError) -> Self {
596 Self::UnableToRemovePushRule
597 }
598}
599
600#[derive(Debug, Error)]
601#[error("expected: {expected}, got: {got:?}")]
602pub struct WrongRoomState {
603 expected: &'static str,
604 got: RoomState,
605}
606
607impl WrongRoomState {
608 pub(crate) fn new(expected: &'static str, got: RoomState) -> Self {
609 Self { expected, got }
610 }
611}