Skip to main content

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