Skip to content

Commit 11bbbd6

Browse files
committed
Custom error for TryFrom<EncryptionInfo>
Currently, we use `anyhow::Error` to reflect problems with the encryption algorithm info. Let's drop the dependency on `anyhow` and tighten up the error handling by defining a custom error type.
1 parent 662e87c commit 11bbbd6

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ matrix-sdk-qrcode = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev
7171
serde = "1.0.91"
7272
serde_json = "1.0.91"
7373
serde-wasm-bindgen = "0.6.5"
74+
thiserror = "2.0.12"
7475
tracing = { version = "0.1.36", default-features = false, features = ["std"] }
7576
tracing-subscriber = { version = "0.3.14", default-features = false, features = ["registry", "std", "ansi"] }
7677
url = "2.5.0"

src/machine.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::{
3838
future::{future_to_promise, future_to_promise_with_custom_error},
3939
identifiers, identities, olm, requests,
4040
requests::{outgoing_request_to_js_value, CrossSigningBootstrapRequests, ToDeviceRequest},
41-
responses::{self, response_from_string},
41+
responses::{self, response_from_string, UnsupportedAlgorithmError},
4242
store,
4343
store::{RoomKeyInfo, RoomKeyWithheldInfo, StoreHandle},
4444
sync_events,
@@ -511,13 +511,15 @@ impl OlmMachine {
511511
.map_err(MegolmDecryptionError::from)?
512512
.into();
513513

514-
responses::DecryptedRoomEvent::try_from(room_event).map_err(|e: anyhow::Error| {
515-
// This happens if we somehow encounter a room event whose encryption info we
516-
// don't understand (e.g., it is encrypted with Olm rather than
517-
// Megolm). That seems pretty unlikely. If it happens, let's
518-
// just treat it as a generic UTD.
519-
MegolmDecryptionError::unable_to_decrypt(format!("{e:#}"))
520-
})
514+
responses::DecryptedRoomEvent::try_from(room_event).map_err(
515+
|e: UnsupportedAlgorithmError| {
516+
// This happens if we somehow encounter a room event whose encryption info we
517+
// don't understand (e.g., it is encrypted with Olm rather than
518+
// Megolm). That seems pretty unlikely. If it happens, let's
519+
// just treat it as a generic UTD.
520+
MegolmDecryptionError::unable_to_decrypt(format!("{e:#}"))
521+
},
522+
)
521523
}))
522524
}
523525

src/responses.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::sync::Arc;
44

5-
use anyhow::anyhow;
65
use js_sys::{Array, JsString};
76
pub(crate) use matrix_sdk_common::ruma::api::client::{
87
backup::add_backup_keys::v3::Response as KeysBackupResponse,
@@ -19,6 +18,7 @@ use matrix_sdk_common::{
1918
ruma::{self, api::IncomingResponse as RumaIncomingResponse},
2019
};
2120
use matrix_sdk_crypto::types::requests::AnyIncomingResponse;
21+
use thiserror::Error;
2222
use wasm_bindgen::prelude::*;
2323

2424
use crate::{encryption, identifiers, requests::RequestType};
@@ -216,7 +216,7 @@ impl DecryptedRoomEvent {
216216
}
217217

218218
impl TryFrom<matrix_sdk_common::deserialized_responses::TimelineEvent> for DecryptedRoomEvent {
219-
type Error = anyhow::Error;
219+
type Error = UnsupportedAlgorithmError;
220220

221221
fn try_from(
222222
value: matrix_sdk_common::deserialized_responses::TimelineEvent,
@@ -288,7 +288,7 @@ impl EncryptionInfo {
288288
}
289289

290290
impl TryFrom<Arc<matrix_sdk_common::deserialized_responses::EncryptionInfo>> for EncryptionInfo {
291-
type Error = anyhow::Error;
291+
type Error = UnsupportedAlgorithmError;
292292

293293
fn try_from(
294294
value: Arc<matrix_sdk_common::deserialized_responses::EncryptionInfo>,
@@ -306,8 +306,8 @@ impl TryFrom<Arc<matrix_sdk_common::deserialized_responses::EncryptionInfo>> for
306306
verification_state: value.verification_state.clone(),
307307
})
308308
}
309-
AlgorithmInfo::OlmV1Curve25519AesSha2 { .. } => Err(anyhow!(
310-
"AlgorithmInfo::OlmV1Curve25519AesSha2 is not applicable for room event EncryptionInfo",
309+
AlgorithmInfo::OlmV1Curve25519AesSha2 { .. } => Err(UnsupportedAlgorithmError(
310+
"AlgorithmInfo::OlmV1Curve25519AesSha2 is not applicable for room event EncryptionInfo".to_owned()
311311
)),
312312
}
313313
}
@@ -344,14 +344,15 @@ pub struct ToDeviceEncryptionInfo {
344344
}
345345

346346
impl TryFrom<matrix_sdk_common::deserialized_responses::EncryptionInfo> for ToDeviceEncryptionInfo {
347-
type Error = anyhow::Error;
347+
type Error = UnsupportedAlgorithmError;
348348

349349
fn try_from(
350350
value: matrix_sdk_common::deserialized_responses::EncryptionInfo,
351351
) -> Result<Self, Self::Error> {
352352
match &value.algorithm_info {
353-
AlgorithmInfo::MegolmV1AesSha2 { .. } => Err(anyhow!(
354-
"AlgorithmInfo::MegolmV1AesSha2 is not applicable for ToDeviceEncryptionInfo",
353+
AlgorithmInfo::MegolmV1AesSha2 { .. } => Err(UnsupportedAlgorithmError(
354+
"AlgorithmInfo::MegolmV1AesSha2 is not applicable for ToDeviceEncryptionInfo"
355+
.to_owned(),
355356
)),
356357
AlgorithmInfo::OlmV1Curve25519AesSha2 { curve25519_public_key_base64 } => Ok(Self {
357358
sender_curve25519_key_base64: curve25519_public_key_base64.clone(),
@@ -375,3 +376,11 @@ impl ToDeviceEncryptionInfo {
375376
)
376377
}
377378
}
379+
380+
/// Error type returned when converting
381+
/// [`matrix_sdk_common::deserialized_responses::EncryptionInfo`] to one of our
382+
/// own types: the `algorithm_info` on the `EncryptionInfo` was of an unexpected
383+
/// type.
384+
#[derive(Error, Debug)]
385+
#[error("{0}")]
386+
pub struct UnsupportedAlgorithmError(String);

0 commit comments

Comments
 (0)