From b597e7121b7ef5876c74fb39c8bcd83c759032cb Mon Sep 17 00:00:00 2001 From: Ian-Bright Date: Fri, 3 May 2024 07:18:36 -0600 Subject: [PATCH 1/3] fix: Change request errors from being parsed into json to being parsed into text --- crates/grapevine_cli/src/http.rs | 47 ++++++++++++++++++-------------- crates/grapevine_cli/src/main.rs | 2 +- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/crates/grapevine_cli/src/http.rs b/crates/grapevine_cli/src/http.rs index 6c2d96c..ed6b274 100644 --- a/crates/grapevine_cli/src/http.rs +++ b/crates/grapevine_cli/src/http.rs @@ -1,8 +1,7 @@ use crate::utils::fs::ACCOUNT_PATH; use babyjubjub_rs::{decompress_point, Point}; use grapevine_common::http::requests::{ - CreateUserRequest, DegreeProofRequest, GetNonceRequest, PhraseRequest, - NewRelationshipRequest, + CreateUserRequest, DegreeProofRequest, GetNonceRequest, NewRelationshipRequest, PhraseRequest, }; use grapevine_common::http::responses::{DegreeData, PhraseCreationResponse}; use grapevine_common::models::ProvingData; @@ -32,7 +31,7 @@ pub async fn get_pubkey_req(username: String) -> Result { Ok(decompress_point(hex::decode(pubkey).unwrap().try_into().unwrap()).unwrap()) } StatusCode::NOT_FOUND => Err(GrapevineError::UserNotFound(username)), - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -45,7 +44,7 @@ pub async fn get_nonce_req(body: GetNonceRequest) -> Result let nonce = res.text().await.unwrap(); Ok(nonce.parse().unwrap()) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -72,7 +71,7 @@ pub async fn get_available_proofs_req( let proofs = res.json::>().await.unwrap(); Ok(proofs) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -100,7 +99,7 @@ pub async fn get_proof_with_params_req( let proof = res.json::().await.unwrap(); Ok(proof) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -117,7 +116,7 @@ pub async fn create_user_req(body: CreateUserRequest) -> Result<(), GrapevineErr let res = client.post(&url).json(&body).send().await.unwrap(); match res.status() { StatusCode::CREATED => return Ok(()), - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -143,6 +142,7 @@ pub async fn add_relationship_req( .send() .await .unwrap(); + println!("Res: {:?}", res); match res.status() { StatusCode::CREATED => { // get message @@ -153,7 +153,7 @@ pub async fn add_relationship_req( .unwrap(); return Ok(message); } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -183,7 +183,8 @@ pub async fn phrase_req( .unwrap(); match res.status() { StatusCode::CREATED => { - let data: PhraseCreationResponse = serde_json::from_str(&res.text().await.unwrap()).unwrap(); + let data: PhraseCreationResponse = + serde_json::from_str(&res.text().await.unwrap()).unwrap(); // increment nonce account .increment_nonce(Some((&**ACCOUNT_PATH).to_path_buf())) @@ -220,7 +221,7 @@ pub async fn get_account_details_req( let details = res.json::<(u64, u64, u64)>().await.unwrap(); Ok(details) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -247,7 +248,7 @@ pub async fn get_degrees_req( let degrees = res.json::>().await.unwrap(); Ok(degrees) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -283,7 +284,7 @@ pub async fn degree_proof_req( .unwrap(); return Ok(()); } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -310,7 +311,7 @@ pub async fn get_known_req( let proofs = res.json::>().await.unwrap(); Ok(proofs) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -338,7 +339,7 @@ pub async fn get_phrase_req( let data = res.json::().await.unwrap(); Ok(data) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -366,11 +367,14 @@ pub async fn show_connections_req( let connection_data = res.json::<(u64, Vec)>().await.unwrap(); Ok(connection_data) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } -pub async fn get_relationships_req(active: bool, account: &mut GrapevineAccount) -> Result, GrapevineError> { +pub async fn get_relationships_req( + active: bool, + account: &mut GrapevineAccount, +) -> Result, GrapevineError> { let route = if active { "active" } else { "pending" }; let url = format!("{}/user/relationship/{}", &**SERVER_URL, route); // produce signature over current nonce @@ -392,11 +396,14 @@ pub async fn get_relationships_req(active: bool, account: &mut GrapevineAccount) let relationships = res.json::>().await.unwrap(); Ok(relationships) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } -pub async fn reject_relationship_req(username: &String, account: &mut GrapevineAccount) -> Result<(), GrapevineError> { +pub async fn reject_relationship_req( + username: &String, + account: &mut GrapevineAccount, +) -> Result<(), GrapevineError> { let url = format!("{}/user/relationship/reject/{}", &**SERVER_URL, username); // produce signature over current nonce let signature = hex::encode(account.sign_nonce().compress()); @@ -416,6 +423,6 @@ pub async fn reject_relationship_req(username: &String, account: &mut GrapevineA .unwrap(); Ok(()) } - _ => Err(res.json::().await.unwrap()), + _ => Err(GrapevineError::Other(res.text().await.unwrap())), } -} \ No newline at end of file +} diff --git a/crates/grapevine_cli/src/main.rs b/crates/grapevine_cli/src/main.rs index 8c8a1b3..de72975 100644 --- a/crates/grapevine_cli/src/main.rs +++ b/crates/grapevine_cli/src/main.rs @@ -142,7 +142,7 @@ pub async fn main() { println!("{}", message); } Err(e) => { - println!("Error: {}", e); + eprintln!("\x1b[0;31merror\x1b[0m: {}", e); } }; } From 7a67ecf64eaf7f536f76ff9d27bf0d9fc04eaeca Mon Sep 17 00:00:00 2001 From: Ian-Bright Date: Fri, 3 May 2024 07:27:33 -0600 Subject: [PATCH 2/3] fix: Nonce mismatch error handled in hacky way --- crates/grapevine_cli/src/http.rs | 46 +++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/crates/grapevine_cli/src/http.rs b/crates/grapevine_cli/src/http.rs index ed6b274..d10dfcb 100644 --- a/crates/grapevine_cli/src/http.rs +++ b/crates/grapevine_cli/src/http.rs @@ -30,6 +30,9 @@ pub async fn get_pubkey_req(username: String) -> Result { let pubkey = res.text().await.unwrap(); Ok(decompress_point(hex::decode(pubkey).unwrap().try_into().unwrap()).unwrap()) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), StatusCode::NOT_FOUND => Err(GrapevineError::UserNotFound(username)), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } @@ -44,6 +47,9 @@ pub async fn get_nonce_req(body: GetNonceRequest) -> Result let nonce = res.text().await.unwrap(); Ok(nonce.parse().unwrap()) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -71,6 +77,9 @@ pub async fn get_available_proofs_req( let proofs = res.json::>().await.unwrap(); Ok(proofs) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -99,6 +108,9 @@ pub async fn get_proof_with_params_req( let proof = res.json::().await.unwrap(); Ok(proof) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -116,6 +128,9 @@ pub async fn create_user_req(body: CreateUserRequest) -> Result<(), GrapevineErr let res = client.post(&url).json(&body).send().await.unwrap(); match res.status() { StatusCode::CREATED => return Ok(()), + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -142,7 +157,6 @@ pub async fn add_relationship_req( .send() .await .unwrap(); - println!("Res: {:?}", res); match res.status() { StatusCode::CREATED => { // get message @@ -153,6 +167,9 @@ pub async fn add_relationship_req( .unwrap(); return Ok(message); } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -191,6 +208,9 @@ pub async fn phrase_req( .unwrap(); return Ok(data); } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => { // Err(res.json::().await.unwrap()) Err(GrapevineError::InternalError) @@ -221,6 +241,9 @@ pub async fn get_account_details_req( let details = res.json::<(u64, u64, u64)>().await.unwrap(); Ok(details) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -248,6 +271,9 @@ pub async fn get_degrees_req( let degrees = res.json::>().await.unwrap(); Ok(degrees) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -284,6 +310,9 @@ pub async fn degree_proof_req( .unwrap(); return Ok(()); } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -311,6 +340,9 @@ pub async fn get_known_req( let proofs = res.json::>().await.unwrap(); Ok(proofs) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -339,6 +371,9 @@ pub async fn get_phrase_req( let data = res.json::().await.unwrap(); Ok(data) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -367,6 +402,9 @@ pub async fn show_connections_req( let connection_data = res.json::<(u64, Vec)>().await.unwrap(); Ok(connection_data) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mistmatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -396,6 +434,9 @@ pub async fn get_relationships_req( let relationships = res.json::>().await.unwrap(); Ok(relationships) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mismatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } @@ -423,6 +464,9 @@ pub async fn reject_relationship_req( .unwrap(); Ok(()) } + StatusCode::UNAUTHORIZED => Err(GrapevineError::Other( + "Nonce mismatch. Syncing, please try again".to_string(), + )), _ => Err(GrapevineError::Other(res.text().await.unwrap())), } } From 40bfbd96181f4b155c8ee257cb6925eba3d98d8a Mon Sep 17 00:00:00 2001 From: Ian-Bright Date: Fri, 3 May 2024 07:31:00 -0600 Subject: [PATCH 3/3] fix: New error type --- crates/grapevine_common/src/errors.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/grapevine_common/src/errors.rs b/crates/grapevine_common/src/errors.rs index 7dc5b05..ca56e1d 100644 --- a/crates/grapevine_common/src/errors.rs +++ b/crates/grapevine_common/src/errors.rs @@ -24,7 +24,8 @@ pub enum GrapevineError { SerdeError(String), DegreeProofExists, DegreeProofVerificationFailed, - FsError(String) + FsError(String), + Other(String), } impl std::fmt::Display for GrapevineError { @@ -46,7 +47,7 @@ impl std::fmt::Display for GrapevineError { } GrapevineError::UserExists(msg) => { write!(f, "User {} already exists with the supplied pubkey", msg) - }, + } GrapevineError::PhraseTooLong => write!(f, "Phrase is too long"), GrapevineError::PendingRelationshipExists(sender, recipient) => { write!( @@ -94,8 +95,9 @@ impl std::fmt::Display for GrapevineError { } GrapevineError::DegreeProofVerificationFailed => { write!(f, "Failed to verify degree proof") - }, + } GrapevineError::FsError(msg) => write!(f, "Filesystem error: {}", msg), + GrapevineError::Other(msg) => write!(f, "{}", msg), } } }