From a194213978ef84d7ccffc77461bdecd8a7016a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 8 Feb 2022 09:43:06 +0100 Subject: [PATCH] rust: Add the string that failed to be parsed as an user id to the error If there's an invalid user id that gets passed to the Rust side we're going to throw an error, this error doesn't tell us what is invalid nor what the string contained. Add the string that is being parsed to the error so that the log line becomes actionable. --- rust-sdk/src/error.rs | 2 ++ rust-sdk/src/lib.rs | 7 +++++++ rust-sdk/src/machine.rs | 30 +++++++++++++++--------------- rust-sdk/src/olm.udl | 1 + 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/rust-sdk/src/error.rs b/rust-sdk/src/error.rs index caa7c44983..1a79c5f6b9 100644 --- a/rust-sdk/src/error.rs +++ b/rust-sdk/src/error.rs @@ -46,6 +46,8 @@ pub enum CryptoStoreError { OlmError(#[from] OlmError), #[error(transparent)] Serialization(#[from] serde_json::Error), + #[error("The given string is not a valid user ID: source {0}, error {1}")] + InvalidUserId(String, RumaIdentifierError), #[error(transparent)] Identifier(#[from] RumaIdentifierError), } diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index 35f62fca4e..533636d0b5 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -17,6 +17,8 @@ mod responses; mod users; mod verification; +use std::convert::TryFrom; + pub use backup_recovery_key::{ BackupRecoveryKey, DecodeError, MegolmV1BackupKey, PassphraseInfo, PkDecryptionError, }; @@ -153,4 +155,9 @@ impl From for CrossSigningStatus { } } +fn parse_user_id(user_id: &str) -> Result, CryptoStoreError> { + Box::::try_from(user_id) + .map_err(|e| CryptoStoreError::InvalidUserId(user_id.to_owned(), e)) +} + include!(concat!(env!("OUT_DIR"), "/olm.uniffi.rs")); diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 441322dca2..3de5800aae 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -48,7 +48,7 @@ use crate::{ CrossSigningStatus, DecodeError, DecryptedEvent, Device, DeviceLists, KeyImportError, KeysImportResult, MegolmV1BackupKey, ProgressListener, QrCode, Request, RequestType, RequestVerificationResult, RoomKeyCounts, ScanResult, SignatureUploadRequest, StartSasResult, - UserIdentity, Verification, VerificationRequest, + UserIdentity, Verification, VerificationRequest, parse_user_id, }; /// A high level state machine that handles E2EE for Matrix. @@ -78,7 +78,7 @@ impl OlmMachine { /// /// * `path` - The path where the state of the machine should be persisted. pub fn new(user_id: &str, device_id: &str, path: &str) -> Result { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; let device_id = device_id.into(); let runtime = Runtime::new().expect("Couldn't create a tokio runtime"); @@ -107,7 +107,7 @@ impl OlmMachine { /// Get a cross signing user identity for the given user ID. pub fn get_identity(&self, user_id: &str) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; Ok( if let Some(identity) = self.runtime.block_on(self.inner.get_identity(&user_id))? { @@ -120,7 +120,7 @@ impl OlmMachine { /// Check if a user identity is considered to be verified by us. pub fn is_identity_verified(&self, user_id: &str) -> Result { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; Ok( if let Some(identity) = self.runtime.block_on(self.inner.get_identity(&user_id))? { @@ -173,7 +173,7 @@ impl OlmMachine { user_id: &str, device_id: &str, ) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; Ok(self .runtime @@ -220,7 +220,7 @@ impl OlmMachine { user_id: &str, device_id: &str, ) -> Result<(), CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; let device = self .runtime @@ -240,7 +240,7 @@ impl OlmMachine { /// /// * `user_id` - The id of the device owner. pub fn get_user_devices(&self, user_id: &str) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; Ok(self .runtime @@ -400,7 +400,7 @@ impl OlmMachine { /// A user can be marked for tracking using the /// [`OlmMachine::update_tracked_users()`] method. pub fn is_user_tracked(&self, user_id: &str) -> Result { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; Ok(self.inner.tracked_users().contains(&user_id)) } @@ -802,7 +802,7 @@ impl OlmMachine { user_id: &str, methods: Vec, ) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; let identity = self.runtime.block_on(self.inner.get_identity(&user_id))?; @@ -845,7 +845,7 @@ impl OlmMachine { event_id: &str, methods: Vec, ) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; let event_id = Box::::try_from(event_id)?; let room_id = Box::::try_from(room_id)?; @@ -883,7 +883,7 @@ impl OlmMachine { device_id: &str, methods: Vec, ) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; let methods = methods.into_iter().map(VerificationMethod::from).collect(); @@ -1010,7 +1010,7 @@ impl OlmMachine { user_id: &str, flow_id: &str, ) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; Ok( if let Some(verification) = self.inner.get_verification(&user_id, flow_id) { @@ -1053,7 +1053,7 @@ impl OlmMachine { user_id: &str, flow_id: &str, ) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; if let Some(verification) = self.inner.get_verification_request(&user_id, flow_id) { Ok(self @@ -1146,7 +1146,7 @@ impl OlmMachine { user_id: &str, flow_id: &str, ) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; Ok( if let Some(verification) = self.inner.get_verification_request(&user_id, flow_id) { @@ -1181,7 +1181,7 @@ impl OlmMachine { user_id: &str, device_id: &str, ) -> Result, CryptoStoreError> { - let user_id = Box::::try_from(user_id)?; + let user_id = parse_user_id(user_id)?; Ok( if let Some(device) = self diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 4adbdd98b9..b93a398233 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -44,6 +44,7 @@ enum CryptoStoreError { "OlmError", "Serialization", "Identifier", + "InvalidUserId", }; [Error]