matrix_sdk_indexeddb/media_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::media::store::{MediaStore, MediaStoreError, MemoryMediaStore};
16use serde::de::Error;
17use thiserror::Error;
18
19use crate::{error::GenericError, transaction::TransactionError};
20
21#[derive(Debug, Error)]
22pub enum IndexeddbMediaStoreError {
23    #[error("unable to open database: {0}")]
24    UnableToOpenDatabase(String),
25    #[error("media store: {0}")]
26    MemoryStore(<MemoryMediaStore as MediaStore>::Error),
27    #[error("transaction: {0}")]
28    Transaction(#[from] TransactionError),
29    #[error("DomException {name} ({code}): {message}")]
30    DomException { name: String, message: String, code: u16 },
31    #[error("cache size too big, cannot exceed 'usize::MAX' ({})", usize::MAX)]
32    CacheSizeTooBig,
33}
34
35impl From<IndexeddbMediaStoreError> for MediaStoreError {
36    fn from(value: IndexeddbMediaStoreError) -> Self {
37        use IndexeddbMediaStoreError::*;
38
39        match value {
40            UnableToOpenDatabase(e) => GenericError::from(e).into(),
41            DomException { .. } => Self::InvalidData { details: value.to_string() },
42            Transaction(inner) => inner.into(),
43            CacheSizeTooBig => GenericError::from(value.to_string()).into(),
44            MemoryStore(error) => error,
45        }
46    }
47}
48
49impl From<web_sys::DomException> for IndexeddbMediaStoreError {
50    fn from(value: web_sys::DomException) -> Self {
51        Self::DomException { name: value.name(), message: value.message(), code: value.code() }
52    }
53}
54
55impl From<indexed_db_futures::error::OpenDbError> for IndexeddbMediaStoreError {
56    fn from(value: indexed_db_futures::error::OpenDbError) -> Self {
57        use indexed_db_futures::error::OpenDbError::*;
58        match value {
59            VersionZero | UnsupportedEnvironment | NullFactory => {
60                Self::UnableToOpenDatabase(value.to_string())
61            }
62            Base(e) => TransactionError::from(e).into(),
63        }
64    }
65}
66
67impl From<TransactionError> for MediaStoreError {
68    fn from(value: TransactionError) -> Self {
69        use TransactionError::*;
70
71        match value {
72            DomException { .. } => Self::InvalidData { details: value.to_string() },
73            Serialization(e) => Self::Serialization(serde_json::Error::custom(e.to_string())),
74            ItemIsNotUnique | ItemNotFound => Self::InvalidData { details: value.to_string() },
75            Backend(e) => GenericError::from(e.to_string()).into(),
76        }
77    }
78}