From e9667a57be0c4ed386499cb4e4af233607246f8f Mon Sep 17 00:00:00 2001 From: notplants Date: Tue, 12 Jul 2022 13:13:22 +0200 Subject: [PATCH 1/2] Change sbot::init to not panic and instead return an error --- Cargo.toml | 2 +- src/error.rs | 6 ++++++ src/sbot.rs | 34 ++++++++++++++++++++++------------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4c7074a..beb2bf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "golgi" -version = "0.2.2" +version = "0.2.3" edition = "2021" authors = ["Max Fowler ", "Andrew Reid "] readme = "README.md" diff --git a/src/error.rs b/src/error.rs index d5efe84..dda2bfa 100644 --- a/src/error.rs +++ b/src/error.rs @@ -34,6 +34,8 @@ pub enum GolgiError { Rpc(RpcError), /// Go-sbot error. Sbot(String), + /// Error initializing Go-sbot. + SbotInit(String), /// SSB sigil-link error. SigilLink(String), /// JSON serialization or deserialization error. @@ -57,6 +59,7 @@ impl std::error::Error for GolgiError { GolgiError::Feed(ref err) => Some(err), GolgiError::Rpc(ref err) => Some(err), GolgiError::Sbot(_) => None, + GolgiError::SbotInit(_) => None, GolgiError::SigilLink(_) => None, GolgiError::SerdeJson(ref err) => Some(err), GolgiError::ContentType(_) => None, @@ -78,6 +81,9 @@ impl std::fmt::Display for GolgiError { // then have the core display msg be: "SSB RPC error: {}", context GolgiError::Rpc(ref err) => write!(f, "SSB RPC failure: {}", err), GolgiError::Sbot(ref err) => write!(f, "Sbot returned an error response: {}", err), + GolgiError::SbotInit(ref err) => { + write!(f, "Sbot encountered an initialization error: {}", err) + } GolgiError::SigilLink(ref context) => write!(f, "SSB blob ID error: {}", context), GolgiError::SerdeJson(_) => write!(f, "Failed to serialize JSON slice"), //GolgiError::WhoAmI(ref err) => write!(f, "{}", err), diff --git a/src/sbot.rs b/src/sbot.rs index 63bdeb4..6875b51 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -75,25 +75,35 @@ impl Sbot { }; let OwnedIdentity { pk, sk, id } = match keystore { - Keystore::Patchwork => keystore::from_patchwork_local() - .await - .expect("couldn't read local patchwork secret from default location"), - Keystore::GoSbot => keystore::from_gosbot_local() - .await - .expect("couldn't read local go-sbot secret from default location"), + Keystore::Patchwork => keystore::from_patchwork_local().await.map_err(|_err| { + GolgiError::SbotInit( + "couldn't read local patchwork secret from default location".to_string(), + ) + })?, + Keystore::GoSbot => keystore::from_gosbot_local().await.map_err(|_err| { + GolgiError::SbotInit( + "couldn't read local go-sbot secret from default location".to_string(), + ) + })?, Keystore::CustomGoSbot(key_path) => { keystore::from_custom_gosbot_keypath(key_path.to_string()) .await - .unwrap_or_else(|_| { - panic!("couldn't read local go-sbot secret from: {}", key_path) - }) + .map_err(|_err| { + GolgiError::SbotInit(format!( + "couldn't read local go-sbot secret from: {}", + key_path + )) + })? } Keystore::CustomPatchwork(key_path) => { keystore::from_custom_patchwork_keypath(key_path.to_string()) .await - .unwrap_or_else(|_| { - panic!("couldn't read local patchwork secret from: {}", key_path) - }) + .map_err(|_err| { + GolgiError::SbotInit(format!( + "couldn't read local patchwork secret from: {}", + key_path + )) + })? } }; From b24f2e4a06c7d0f33a014ff8a3e0375b34e9fb26 Mon Sep 17 00:00:00 2001 From: notplants Date: Fri, 15 Jul 2022 11:16:46 +0200 Subject: [PATCH 2/2] Combine Sbot and SbotInit errors --- src/error.rs | 8 +------- src/sbot.rs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/error.rs b/src/error.rs index dda2bfa..4e29f1e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -34,8 +34,6 @@ pub enum GolgiError { Rpc(RpcError), /// Go-sbot error. Sbot(String), - /// Error initializing Go-sbot. - SbotInit(String), /// SSB sigil-link error. SigilLink(String), /// JSON serialization or deserialization error. @@ -59,7 +57,6 @@ impl std::error::Error for GolgiError { GolgiError::Feed(ref err) => Some(err), GolgiError::Rpc(ref err) => Some(err), GolgiError::Sbot(_) => None, - GolgiError::SbotInit(_) => None, GolgiError::SigilLink(_) => None, GolgiError::SerdeJson(ref err) => Some(err), GolgiError::ContentType(_) => None, @@ -80,10 +77,7 @@ impl std::fmt::Display for GolgiError { // TODO: improve this variant with a context message // then have the core display msg be: "SSB RPC error: {}", context GolgiError::Rpc(ref err) => write!(f, "SSB RPC failure: {}", err), - GolgiError::Sbot(ref err) => write!(f, "Sbot returned an error response: {}", err), - GolgiError::SbotInit(ref err) => { - write!(f, "Sbot encountered an initialization error: {}", err) - } + GolgiError::Sbot(ref err) => write!(f, "Sbot encountered an error: {}", err), GolgiError::SigilLink(ref context) => write!(f, "SSB blob ID error: {}", context), GolgiError::SerdeJson(_) => write!(f, "Failed to serialize JSON slice"), //GolgiError::WhoAmI(ref err) => write!(f, "{}", err), diff --git a/src/sbot.rs b/src/sbot.rs index 6875b51..d4f4805 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -76,21 +76,21 @@ impl Sbot { let OwnedIdentity { pk, sk, id } = match keystore { Keystore::Patchwork => keystore::from_patchwork_local().await.map_err(|_err| { - GolgiError::SbotInit( - "couldn't read local patchwork secret from default location".to_string(), + GolgiError::Sbot( + "sbot initialization error: couldn't read local patchwork secret from default location".to_string(), ) })?, Keystore::GoSbot => keystore::from_gosbot_local().await.map_err(|_err| { - GolgiError::SbotInit( - "couldn't read local go-sbot secret from default location".to_string(), + GolgiError::Sbot( + "sbot initialization error: couldn't read local go-sbot secret from default location".to_string(), ) })?, Keystore::CustomGoSbot(key_path) => { keystore::from_custom_gosbot_keypath(key_path.to_string()) .await .map_err(|_err| { - GolgiError::SbotInit(format!( - "couldn't read local go-sbot secret from: {}", + GolgiError::Sbot(format!( + "sbot initialization error: couldn't read local go-sbot secret from: {}", key_path )) })? @@ -99,8 +99,8 @@ impl Sbot { keystore::from_custom_patchwork_keypath(key_path.to_string()) .await .map_err(|_err| { - GolgiError::SbotInit(format!( - "couldn't read local patchwork secret from: {}", + GolgiError::Sbot(format!( + "sbot initialization error: couldn't read local patchwork secret from: {}", key_path )) })?