matrix_sdk_crypto/store/
error.rs

1// Copyright 2023 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
15use std::{convert::Infallible, fmt::Debug, io::Error as IoError};
16
17use ruma::{IdParseError, OwnedDeviceId, OwnedUserId};
18use serde_json::Error as SerdeError;
19use thiserror::Error;
20
21use crate::olm::SessionCreationError;
22
23/// A `CryptoStore` specific result type.
24pub type Result<T, E = CryptoStoreError> = std::result::Result<T, E>;
25
26/// The crypto store's error type.
27#[derive(Debug, Error)]
28pub enum CryptoStoreError {
29    /// The account that owns the sessions, group sessions, and devices wasn't
30    /// found.
31    #[error("can't save/load sessions or group sessions in the store before an account is stored")]
32    AccountUnset,
33
34    /// The store doesn't support multiple accounts and data from another device
35    /// was discovered.
36    #[error(
37        "the account in the store doesn't match the account in the constructor: \
38        expected {}:{}, got {}:{}", .expected.0, .expected.1, .got.0, .got.1
39    )]
40    MismatchedAccount {
41        /// The expected user/device id pair.
42        expected: (OwnedUserId, OwnedDeviceId),
43        /// The user/device id pair that was loaded from the store.
44        got: (OwnedUserId, OwnedDeviceId),
45    },
46
47    /// An IO error occurred.
48    #[error(transparent)]
49    Io(#[from] IoError),
50
51    /// Failed to decrypt an pickled object.
52    #[error("An object failed to be decrypted while unpickling")]
53    UnpicklingError,
54
55    /// Failed to decrypt an pickled object.
56    #[error(transparent)]
57    Pickle(#[from] vodozemac::PickleError),
58
59    /// The received room key couldn't be converted into a valid Megolm session.
60    #[error(transparent)]
61    SessionCreation(#[from] SessionCreationError),
62
63    /// A Matrix identifier failed to be validated.
64    #[error(transparent)]
65    IdentifierValidation(#[from] IdParseError),
66
67    /// The store failed to (de)serialize a data type.
68    #[error(transparent)]
69    Serialization(#[from] SerdeError),
70
71    /// The database format has changed in a backwards incompatible way.
72    #[error(
73        "The database format changed in an incompatible way, current \
74        version: {0}, latest version: {1}"
75    )]
76    UnsupportedDatabaseVersion(usize, usize),
77
78    /// A problem with the underlying database backend
79    #[error(transparent)]
80    #[cfg(not(target_family = "wasm"))]
81    Backend(Box<dyn std::error::Error + Send + Sync>),
82
83    /// A problem with the underlying database backend
84    #[error(transparent)]
85    #[cfg(target_family = "wasm")]
86    Backend(Box<dyn std::error::Error>),
87
88    /// An error due to an invalid generation in a cross-process locking scheme.
89    #[error("invalid lock generation: {0}")]
90    InvalidLockGeneration(String),
91}
92
93impl CryptoStoreError {
94    /// Create a new [`Backend`][Self::Backend] error.
95    ///
96    /// Shorthand for `StoreError::Backend(Box::new(error))`.
97    #[inline]
98    #[cfg(not(target_family = "wasm"))]
99    pub fn backend<E>(error: E) -> Self
100    where
101        E: std::error::Error + Send + Sync + 'static,
102    {
103        Self::Backend(Box::new(error))
104    }
105
106    /// Create a new [`Backend`][Self::Backend] error.
107    ///
108    /// Shorthand for `StoreError::Backend(Box::new(error))`.
109    #[inline]
110    #[cfg(target_family = "wasm")]
111    pub fn backend<E>(error: E) -> Self
112    where
113        E: std::error::Error + 'static,
114    {
115        Self::Backend(Box::new(error))
116    }
117}
118
119impl From<Infallible> for CryptoStoreError {
120    fn from(never: Infallible) -> Self {
121        match never {}
122    }
123}