matrix_sdk_indexeddb/error.rs
1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License
14
15#[cfg(feature = "state-store")]
16use matrix_sdk_base::StoreError;
17#[cfg(feature = "event-cache-store")]
18use matrix_sdk_base::event_cache::store::EventCacheStoreError;
19#[cfg(feature = "media-store")]
20use matrix_sdk_base::media::store::MediaStoreError;
21#[cfg(any(feature = "event-cache-store", feature = "media-store"))]
22use matrix_sdk_base::{SendOutsideWasm, SyncOutsideWasm};
23#[cfg(feature = "e2e-encryption")]
24use matrix_sdk_crypto::CryptoStoreError;
25use thiserror::Error;
26
27/// A trait that combines the necessary traits needed for asynchronous runtimes,
28/// but excludes them when running in a web environment - i.e., when
29/// `#[cfg(target_family = "wasm")]`.
30#[cfg(any(feature = "event-cache-store", feature = "media-store"))]
31pub trait AsyncErrorDeps: std::error::Error + SendOutsideWasm + SyncOutsideWasm + 'static {}
32
33#[cfg(any(feature = "event-cache-store", feature = "media-store"))]
34impl<T> AsyncErrorDeps for T where T: std::error::Error + SendOutsideWasm + SyncOutsideWasm + 'static
35{}
36
37/// A wrapper around [`String`] that derives [`Error`](std::error::Error).
38/// This is useful when a particular error is not [`Send`] or [`Sync`] but
39/// must be mapped into a higher-level error that requires those constraints,
40/// e.g. [`StoreError::Backend`], [`CryptStoreError::Backend`], etc.
41#[derive(Debug, Error)]
42#[error("{0}")]
43pub struct GenericError(String);
44
45impl<S: AsRef<str>> From<S> for GenericError {
46 fn from(value: S) -> Self {
47 Self(value.as_ref().to_owned())
48 }
49}
50
51#[cfg(feature = "e2e-encryption")]
52impl From<GenericError> for CryptoStoreError {
53 fn from(value: GenericError) -> Self {
54 Self::backend(value)
55 }
56}
57
58#[cfg(feature = "event-cache-store")]
59impl From<GenericError> for EventCacheStoreError {
60 fn from(value: GenericError) -> Self {
61 Self::backend(value)
62 }
63}
64
65#[cfg(feature = "media-store")]
66impl From<GenericError> for MediaStoreError {
67 fn from(value: GenericError) -> Self {
68 Self::backend(value)
69 }
70}
71
72#[cfg(feature = "state-store")]
73impl From<GenericError> for StoreError {
74 fn from(value: GenericError) -> Self {
75 Self::backend(value)
76 }
77}