matrix_sdk_indexeddb/event_cache_store/
error.rs1use 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}