From a399840dffcb238c07c2726cf5c2f10de2df276e Mon Sep 17 00:00:00 2001 From: Michael Goldenberg Date: Wed, 9 Jul 2025 12:38:01 -0400 Subject: [PATCH] refactor(indexeddb): separate transaction and event cache error conversions Signed-off-by: Michael Goldenberg --- .../src/event_cache_store/error.rs | 29 ++++++------------- .../src/event_cache_store/transaction.rs | 23 +++++++++++---- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/error.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/error.rs index 4bc6a847d..b8fb67dfd 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/error.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/error.rs @@ -59,27 +59,16 @@ impl From for IndexeddbEventCacheStoreError { impl From for EventCacheStoreError { fn from(value: IndexeddbEventCacheStoreError) -> Self { + use IndexeddbEventCacheStoreError::*; + match value { - IndexeddbEventCacheStoreError::DomException { .. } - | IndexeddbEventCacheStoreError::ChunksContainCycle - | IndexeddbEventCacheStoreError::ChunksContainDisjointLists - | IndexeddbEventCacheStoreError::NoMaxChunkId - | IndexeddbEventCacheStoreError::UnableToLoadChunk => { - Self::InvalidData { details: value.to_string() } - } - IndexeddbEventCacheStoreError::Transaction(ref inner) => match inner { - IndexeddbEventCacheStoreTransactionError::DomException { .. } => { - Self::InvalidData { details: value.to_string() } - } - IndexeddbEventCacheStoreTransactionError::Serialization(e) => { - Self::Serialization(serde_json::Error::custom(e.to_string())) - } - IndexeddbEventCacheStoreTransactionError::ItemIsNotUnique - | IndexeddbEventCacheStoreTransactionError::ItemNotFound => { - Self::InvalidData { details: value.to_string() } - } - }, - IndexeddbEventCacheStoreError::MemoryStore(inner) => inner, + DomException { .. } + | ChunksContainCycle + | ChunksContainDisjointLists + | NoMaxChunkId + | UnableToLoadChunk => Self::InvalidData { details: value.to_string() }, + Transaction(inner) => inner.into(), + MemoryStore(inner) => inner, } } } diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs index b0e96ee7b..45615d0ae 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/transaction.rs @@ -14,11 +14,14 @@ use indexed_db_futures::{prelude::IdbTransaction, IdbQuerySource}; use matrix_sdk_base::{ - event_cache::{Event as RawEvent, Gap as RawGap}, + event_cache::{store::EventCacheStoreError, Event as RawEvent, Gap as RawGap}, linked_chunk::{ChunkContent, ChunkIdentifier, RawChunk}, }; use ruma::{events::relation::RelationType, OwnedEventId, RoomId}; -use serde::{de::DeserializeOwned, Serialize}; +use serde::{ + de::{DeserializeOwned, Error}, + Serialize, +}; use thiserror::Error; use web_sys::IdbCursorDirection; @@ -55,9 +58,19 @@ impl From for IndexeddbEventCacheStoreTransactionError { impl From for IndexeddbEventCacheStoreTransactionError { fn from(e: serde_wasm_bindgen::Error) -> Self { - Self::Serialization(Box::new(::custom( - e.to_string(), - ))) + Self::Serialization(Box::new(serde_json::Error::custom(e.to_string()))) + } +} + +impl From for EventCacheStoreError { + fn from(value: IndexeddbEventCacheStoreTransactionError) -> Self { + use IndexeddbEventCacheStoreTransactionError::*; + + match value { + DomException { .. } => Self::InvalidData { details: value.to_string() }, + Serialization(e) => Self::Serialization(serde_json::Error::custom(e.to_string())), + ItemIsNotUnique | ItemNotFound => Self::InvalidData { details: value.to_string() }, + } } }