matrix_sdk_indexeddb/event_cache_store/
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
15use matrix_sdk_base::event_cache::store::EventCacheStoreError;
16use serde::de::Error;
17use thiserror::Error;
18
19use crate::{error::GenericError, transaction::TransactionError};
20
21#[derive(Debug, Error)]
22pub enum IndexeddbEventCacheStoreError {
23    #[error("unable to open database: {0}")]
24    UnableToOpenDatabase(String),
25    #[error("DomException {name} ({code}): {message}")]
26    DomException { name: String, message: String, code: u16 },
27    #[error("chunks contain disjoint lists")]
28    ChunksContainDisjointLists,
29    #[error("chunks contain cycle")]
30    ChunksContainCycle,
31    #[error("unable to load chunk")]
32    UnableToLoadChunk,
33    #[error("no max chunk id")]
34    NoMaxChunkId,
35    #[error("transaction: {0}")]
36    Transaction(#[from] TransactionError),
37}
38
39impl From<web_sys::DomException> for IndexeddbEventCacheStoreError {
40    fn from(value: web_sys::DomException) -> IndexeddbEventCacheStoreError {
41        IndexeddbEventCacheStoreError::DomException {
42            name: value.name(),
43            message: value.message(),
44            code: value.code(),
45        }
46    }
47}
48
49impl From<indexed_db_futures::error::OpenDbError> for IndexeddbEventCacheStoreError {
50    fn from(value: indexed_db_futures::error::OpenDbError) -> Self {
51        use indexed_db_futures::error::OpenDbError::*;
52        match value {
53            VersionZero | UnsupportedEnvironment | NullFactory => {
54                Self::UnableToOpenDatabase(value.to_string())
55            }
56            Base(e) => TransactionError::from(e).into(),
57        }
58    }
59}
60
61impl From<IndexeddbEventCacheStoreError> for EventCacheStoreError {
62    fn from(value: IndexeddbEventCacheStoreError) -> Self {
63        use IndexeddbEventCacheStoreError::*;
64
65        match value {
66            UnableToOpenDatabase(e) => GenericError::from(e).into(),
67            DomException { .. }
68            | ChunksContainCycle
69            | ChunksContainDisjointLists
70            | NoMaxChunkId
71            | UnableToLoadChunk => Self::InvalidData { details: value.to_string() },
72            Transaction(inner) => inner.into(),
73        }
74    }
75}
76
77impl From<TransactionError> for EventCacheStoreError {
78    fn from(value: TransactionError) -> Self {
79        use TransactionError::*;
80
81        match value {
82            DomException { .. } => Self::InvalidData { details: value.to_string() },
83            Serialization(e) => Self::Serialization(serde_json::Error::custom(e.to_string())),
84            ItemIsNotUnique | ItemNotFound | NumericalOverflow => {
85                Self::InvalidData { details: value.to_string() }
86            }
87            Backend(e) => GenericError::from(e.to_string()).into(),
88        }
89    }
90}