Skip to content

Commit af90b7a

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): add conversions into IndexeddbSerializerError
Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
1 parent 3f3daef commit af90b7a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

crates/matrix-sdk-indexeddb/src/crypto_store/indexeddb_serializer.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ pub enum IndexeddbSerializerError {
5656
CryptoStoreError(#[from] CryptoStoreError),
5757
}
5858

59+
impl From<web_sys::DomException> for IndexeddbSerializerError {
60+
fn from(frm: web_sys::DomException) -> Self {
61+
Self::DomException { name: frm.name(), message: frm.message(), code: frm.code() }
62+
}
63+
}
64+
65+
impl From<serde_wasm_bindgen::Error> for IndexeddbSerializerError {
66+
fn from(e: serde_wasm_bindgen::Error) -> Self {
67+
Self::Serialization(serde::de::Error::custom(e.to_string()))
68+
}
69+
}
70+
5971
#[derive(Debug, Deserialize, Serialize)]
6072
#[serde(untagged)]
6173
pub enum MaybeEncrypted {
@@ -107,15 +119,15 @@ impl IndexeddbSerializer {
107119
&self,
108120
table_name: &str,
109121
key: T,
110-
) -> Result<IdbKeyRange, IndexeddbCryptoStoreError>
122+
) -> Result<IdbKeyRange, IndexeddbSerializerError>
111123
where
112124
T: SafeEncode,
113125
{
114126
match &self.store_cipher {
115127
Some(cipher) => key.encode_to_range_secure(table_name, cipher),
116128
None => key.encode_to_range(),
117129
}
118-
.map_err(|e| IndexeddbCryptoStoreError::DomException {
130+
.map_err(|e| IndexeddbSerializerError::DomException {
119131
code: 0,
120132
name: "IdbKeyRangeMakeError".to_owned(),
121133
message: e,
@@ -130,7 +142,7 @@ impl IndexeddbSerializer {
130142
pub fn serialize_value(
131143
&self,
132144
value: &impl Serialize,
133-
) -> Result<JsValue, IndexeddbCryptoStoreError> {
145+
) -> Result<JsValue, IndexeddbSerializerError> {
134146
let serialized = self.maybe_encrypt_value(value)?;
135147
Ok(serde_wasm_bindgen::to_value(&serialized)?)
136148
}
@@ -184,7 +196,7 @@ impl IndexeddbSerializer {
184196
pub fn deserialize_value<T: DeserializeOwned>(
185197
&self,
186198
value: JsValue,
187-
) -> Result<T, IndexeddbCryptoStoreError> {
199+
) -> Result<T, IndexeddbSerializerError> {
188200
// Objects which are serialized nowadays should be represented as a
189201
// `MaybeEncrypted`. However, `serialize_value` previously used a
190202
// different format, so we need to handle that in case we have old data.
@@ -238,11 +250,11 @@ impl IndexeddbSerializer {
238250
pub fn deserialize_legacy_value<T: DeserializeOwned>(
239251
&self,
240252
value: JsValue,
241-
) -> Result<T, IndexeddbCryptoStoreError> {
253+
) -> Result<T, IndexeddbSerializerError> {
242254
match &self.store_cipher {
243255
Some(cipher) => {
244256
if !value.is_array() {
245-
return Err(IndexeddbCryptoStoreError::CryptoStoreError(
257+
return Err(IndexeddbSerializerError::CryptoStoreError(
246258
CryptoStoreError::UnpicklingError,
247259
));
248260
}

crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ impl_crypto_store! {
11561156
.object_store(keys::DEVICES)?
11571157
.get(&key)?
11581158
.await?
1159-
.map(|i| self.serializer.deserialize_value(i))
1159+
.map(|i| self.serializer.deserialize_value(i).map_err(Into::into))
11601160
.transpose()
11611161
}
11621162

@@ -1193,7 +1193,7 @@ impl_crypto_store! {
11931193
.object_store(keys::IDENTITIES)?
11941194
.get(&self.serializer.encode_key(keys::IDENTITIES, user_id))?
11951195
.await?
1196-
.map(|i| self.serializer.deserialize_value(i))
1196+
.map(|i| self.serializer.deserialize_value(i).map_err(Into::into))
11971197
.transpose()
11981198
}
11991199

@@ -1370,7 +1370,7 @@ impl_crypto_store! {
13701370
.object_store(keys::ROOM_SETTINGS)?
13711371
.get(&key)?
13721372
.await?
1373-
.map(|v| self.serializer.deserialize_value(v))
1373+
.map(|v| self.serializer.deserialize_value(v).map_err(Into::into))
13741374
.transpose()
13751375
}
13761376

@@ -1387,7 +1387,7 @@ impl_crypto_store! {
13871387
.object_store(keys::CORE)?
13881388
.get(&JsValue::from_str(key))?
13891389
.await?
1390-
.map(|v| self.serializer.deserialize_value(v))
1390+
.map(|v| self.serializer.deserialize_value(v).map_err(Into::into))
13911391
.transpose()
13921392
}
13931393

0 commit comments

Comments
 (0)