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