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::transaction::TransactionError;
20
21#[derive(Debug, Error)]
22pub enum IndexeddbMediaStoreError {
23    #[error("media store: {0}")]
24    MemoryStore(<MemoryMediaStore as MediaStore>::Error),
25
26    #[error("transaction: {0}")]
27    Transaction(#[from] TransactionError),
28
29    #[error("DomException {name} ({code}): {message}")]
30    DomException { name: String, message: String, code: u16 },
31}
32
33impl From<IndexeddbMediaStoreError> for MediaStoreError {
34    fn from(value: IndexeddbMediaStoreError) -> Self {
35        use IndexeddbMediaStoreError::*;
36
37        match value {
38            DomException { .. } => Self::InvalidData { details: value.to_string() },
39            Transaction(inner) => inner.into(),
40            MemoryStore(error) => error,
41        }
42    }
43}
44
45impl From<web_sys::DomException> for IndexeddbMediaStoreError {
46    fn from(value: web_sys::DomException) -> Self {
47        Self::DomException { name: value.name(), message: value.message(), code: value.code() }
48    }
49}
50
51impl From<TransactionError> for MediaStoreError {
52    fn from(value: TransactionError) -> Self {
53        use TransactionError::*;
54
55        match value {
56            DomException { .. } => Self::InvalidData { details: value.to_string() },
57            Serialization(e) => Self::Serialization(serde_json::Error::custom(e.to_string())),
58            ItemIsNotUnique | ItemNotFound => Self::InvalidData { details: value.to_string() },
59        }
60    }
61}