Skip to content

Commit

Permalink
chore(bindings): Replace various expectations with anyhow contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanceriu authored Oct 19, 2022
1 parent c92d946 commit 8470b49
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{Arc, RwLock};

use anyhow::anyhow;
use anyhow::{anyhow, Context};
use matrix_sdk::{
config::SyncSettings,
media::{MediaFormat, MediaRequest, MediaThumbnailSize},
Expand Down Expand Up @@ -129,7 +129,7 @@ impl Client {

pub fn restore_token(&self) -> anyhow::Result<String> {
RUNTIME.block_on(async move {
let session = self.client.session().expect("Missing session");
let session = self.client.session().context("Missing session")?;
let homeurl = self.client.homeserver().await.into();
Ok(serde_json::to_string(&RestoreToken {
session,
Expand All @@ -141,28 +141,28 @@ impl Client {
}

pub fn user_id(&self) -> anyhow::Result<String> {
let user_id = self.client.user_id().expect("No User ID found");
let user_id = self.client.user_id().context("No User ID found")?;
Ok(user_id.to_string())
}

pub fn display_name(&self) -> anyhow::Result<String> {
let l = self.client.clone();
RUNTIME.block_on(async move {
let display_name = l.account().get_display_name().await?.expect("No User ID found");
let display_name = l.account().get_display_name().await?.context("No User ID found")?;
Ok(display_name)
})
}

pub fn avatar_url(&self) -> anyhow::Result<String> {
let l = self.client.clone();
RUNTIME.block_on(async move {
let avatar_url = l.account().get_avatar_url().await?.expect("No User ID found");
let avatar_url = l.account().get_avatar_url().await?.context("No User ID found")?;
Ok(avatar_url.to_string())
})
}

pub fn device_id(&self) -> anyhow::Result<String> {
let device_id = self.client.device_id().expect("No Device ID found");
let device_id = self.client.device_id().context("No Device ID found")?;
Ok(device_id.to_string())
}

Expand Down Expand Up @@ -235,13 +235,13 @@ impl Client {
return Ok(Arc::new(session_verification_controller.clone()));
}

let user_id = self.client.user_id().expect("Failed retrieving current user_id");
let user_id = self.client.user_id().context("Failed retrieving current user_id")?;
let user_identity = self
.client
.encryption()
.get_user_identity(user_id)
.await?
.expect("Failed retrieving user identity");
.context("Failed retrieving user identity")?;

let session_verification_controller = SessionVerificationController::new(user_identity);

Expand Down
8 changes: 4 additions & 4 deletions bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl Room {
let room = self.room.clone();
let user_id = user_id;
RUNTIME.block_on(async move {
let user_id = <&UserId>::try_from(&*user_id).expect("Invalid user id.");
let member = room.get_member(user_id).await?.expect("No user found");
let user_id = <&UserId>::try_from(&*user_id).context("Invalid user id.")?;
let member = room.get_member(user_id).await?.context("No user found")?;
let avatar_url_string = member.avatar_url().map(|m| m.to_string());
Ok(avatar_url_string)
})
Expand All @@ -103,8 +103,8 @@ impl Room {
let room = self.room.clone();
let user_id = user_id;
RUNTIME.block_on(async move {
let user_id = <&UserId>::try_from(&*user_id).expect("Invalid user id.");
let member = room.get_member(user_id).await?.expect("No user found");
let user_id = <&UserId>::try_from(&*user_id).context("Invalid user id.")?;
let member = room.get_member(user_id).await?.context("No user found")?;
let avatar_url_string = member.display_name().map(|m| m.to_owned());
Ok(avatar_url_string)
})
Expand Down

0 comments on commit 8470b49

Please sign in to comment.