From ba0f5ee81d8217430576e798b8f5d501e24ed9fe Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 21 May 2022 13:04:53 +0000 Subject: [PATCH] securejoin: replace thiserror with anyhow Refactoring to reduce the number of custom error types. --- CHANGELOG.md | 2 +- src/securejoin.rs | 34 +++++++--------------------------- src/securejoin/bob.rs | 11 ++++------- src/securejoin/bobstate.rs | 14 ++++---------- 4 files changed, 16 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa464be09..221dcdeb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased ### Changes -- refactorings #3354 #3347 #3353 +- refactorings #3354 #3347 #3353 #3346 ### Fixes - do not unnecessarily SELECT folders if there are no operations planned on diff --git a/src/securejoin.rs b/src/securejoin.rs index dfa061e94..7100541d1 100644 --- a/src/securejoin.rs +++ b/src/securejoin.rs @@ -137,27 +137,13 @@ async fn get_self_fingerprint(context: &Context) -> Option { } } -#[derive(Debug, thiserror::Error)] -pub enum JoinError { - #[error("Failed to send handshake message: {0}")] - SendMessage(#[from] SendMsgError), - - // Note that this can currently only occur if there is a bug in the QR/Lot code as this - // is supposed to create a contact for us. - #[error("Unknown contact (this is a bug): {0}")] - UnknownContact(#[source] anyhow::Error), - - #[error("Other")] - Other(#[from] anyhow::Error), -} - /// Take a scanned QR-code and do the setup-contact/join-group/invite handshake. /// /// This is the start of the process for the joiner. See the module and ffi documentation /// for more details. /// /// The function returns immediately and the handshake will run in background. -pub async fn dc_join_securejoin(context: &Context, qr: &str) -> Result { +pub async fn dc_join_securejoin(context: &Context, qr: &str) -> Result { securejoin(context, qr).await.map_err(|err| { warn!(context, "Fatal joiner error: {:#}", err); // The user just scanned this QR code so has context on what failed. @@ -166,7 +152,7 @@ pub async fn dc_join_securejoin(context: &Context, qr: &str) -> Result Result { +async fn securejoin(context: &Context, qr: &str) -> Result { /*======================================================== ==== Bob - the joiner's side ===== ==== Step 2 in "Setup verified contact" protocol ===== @@ -180,14 +166,6 @@ async fn securejoin(context: &Context, qr: &str) -> Result { bob::start_protocol(context, invite).await } -/// Error when failing to send a protocol handshake message. -/// -/// Wrapping the [anyhow::Error] means we can "impl From" more easily on errors from this -/// function. -#[derive(Debug, thiserror::Error)] -#[error("Failed sending handshake message")] -pub struct SendMsgError(#[from] anyhow::Error); - /// Send handshake message from Alice's device; /// Bob's handshake messages are sent in `BobState::send_handshake_message()`. async fn send_alice_handshake_msg( @@ -195,7 +173,7 @@ async fn send_alice_handshake_msg( contact_id: ContactId, step: &str, fingerprint: Option, -) -> Result<(), SendMsgError> { +) -> Result<()> { let mut msg = Message { viewtype: Viewtype::Text, text: Some(format!("Secure-Join: {}", step)), @@ -353,7 +331,8 @@ pub(crate) async fn handle_securejoin_handshake( &format!("{}-auth-required", &step[..2]), None, ) - .await?; + .await + .context("failed sending auth-required handshake message")?; Ok(HandshakeMessage::Done) } "vg-auth-required" | "vc-auth-required" => { @@ -481,7 +460,8 @@ pub(crate) async fn handle_securejoin_handshake( "vc-contact-confirm", Some(fingerprint), ) - .await?; + .await + .context("failed sending vc-contact-confirm message")?; inviter_progress!(context, contact_id, 1000); } diff --git a/src/securejoin/bob.rs b/src/securejoin/bob.rs index 110981886..e99c7c196 100644 --- a/src/securejoin/bob.rs +++ b/src/securejoin/bob.rs @@ -3,7 +3,7 @@ //! This are some helper functions around [`BobState`] which augment the state changes with //! the required user interactions. -use anyhow::Result; +use anyhow::{Context as _, Result}; use crate::chat::{is_contact_in_chat, ChatId, ProtectionStatus}; use crate::constants::{Blocked, Chattype}; @@ -16,7 +16,7 @@ use crate::{chat, stock_str}; use super::bobstate::{BobHandshakeStage, BobState}; use super::qrinvite::QrInvite; -use super::{HandshakeMessage, JoinError}; +use super::HandshakeMessage; /// Starts the securejoin protocol with the QR `invite`. /// @@ -30,10 +30,7 @@ use super::{HandshakeMessage, JoinError}; /// /// The [`ChatId`] of the created chat is returned, for a SetupContact QR this is the 1:1 /// chat with Alice, for a SecureJoin QR this is the group chat. -pub(super) async fn start_protocol( - context: &Context, - invite: QrInvite, -) -> Result { +pub(super) async fn start_protocol(context: &Context, invite: QrInvite) -> Result { // A 1:1 chat is needed to send messages to Alice. When joining a group this chat is // hidden, if a user starts sending messages in it it will be unhidden in // dc_receive_imf. @@ -43,7 +40,7 @@ pub(super) async fn start_protocol( }; let chat_id = ChatId::create_for_contact_with_blocked(context, invite.contact_id(), hidden) .await - .map_err(JoinError::UnknownContact)?; + .with_context(|| format!("can't create chat for contact {}", invite.contact_id()))?; // Now start the protocol and initialise the state let (state, stage, aborted_states) = diff --git a/src/securejoin/bobstate.rs b/src/securejoin/bobstate.rs index 66b9bc58b..1fd02ebea 100644 --- a/src/securejoin/bobstate.rs +++ b/src/securejoin/bobstate.rs @@ -22,9 +22,7 @@ use crate::param::Param; use crate::sql::Sql; use super::qrinvite::QrInvite; -use super::{ - encrypted_and_signed, fingerprint_equals_sender, mark_peer_as_verified, JoinError, SendMsgError, -}; +use super::{encrypted_and_signed, fingerprint_equals_sender, mark_peer_as_verified}; /// The stage of the [`BobState`] securejoin handshake protocol state machine. /// @@ -94,7 +92,7 @@ impl BobState { context: &Context, invite: QrInvite, chat_id: ChatId, - ) -> Result<(Self, BobHandshakeStage, Vec), JoinError> { + ) -> Result<(Self, BobHandshakeStage, Vec)> { let (stage, next) = if fingerprint_equals_sender(context, invite.fingerprint(), invite.contact_id()).await? { @@ -404,11 +402,7 @@ impl BobState { /// Sends the requested handshake message to Alice. /// /// This takes care of adding the required headers for the step. - async fn send_handshake_message( - &self, - context: &Context, - step: BobHandshakeMsg, - ) -> Result<(), SendMsgError> { + async fn send_handshake_message(&self, context: &Context, step: BobHandshakeMsg) -> Result<()> { send_handshake_message(context, &self.invite, self.chat_id, step).await } } @@ -422,7 +416,7 @@ async fn send_handshake_message( invite: &QrInvite, chat_id: ChatId, step: BobHandshakeMsg, -) -> Result<(), SendMsgError> { +) -> Result<()> { let mut msg = Message { viewtype: Viewtype::Text, text: Some(step.body_text(invite)),