better error handling in isync functions

This commit is contained in:
Mystikfluu 2023-04-02 12:59:59 +02:00
parent b2e8ad58d5
commit 3326ee886b

View File

@ -53,6 +53,12 @@ fn sha256hexhash(data: Vec<u8>) -> String {
} }
pub async fn isync_compare_hashes() -> bool { pub async fn isync_compare_hashes() -> bool {
/*!
* \brief compare local hash to remote hash
* \return true if hashes are equal
* \return false if hashes are not equal
* \return true if error
*/
let hash = sha256hexhash(export_data().unwrap()); let hash = sha256hexhash(export_data().unwrap());
let token = get_token(); let token = get_token();
@ -62,14 +68,29 @@ pub async fn isync_compare_hashes() -> bool {
let req = client.get("https://ipass.ipost.rocks/hash") let req = client.get("https://ipass.ipost.rocks/hash")
.header("ipass-auth-token", token) .header("ipass-auth-token", token)
.timeout(std::time::Duration::from_secs(3)) .timeout(std::time::Duration::from_secs(3))
.build().unwrap(); .build();
let res = client.execute(req).await.unwrap(); if let Ok(req) = req {
let body = res.json::<HashRes>().await.unwrap(); let res = client.execute(req).await;
if body.success { if let Ok(res) = res {
println!("Hash: {} {}", hash, body.hash); let body = res.json::<HashRes>().await;
body.hash == hash if let Ok(body) = body {
if body.success {
//println!("Hash: {} {}", hash, body.hash);
body.hash == hash
} else {
eprintln!("Error: {}", body.errorcode);
true
}
} else {
eprintln!("Error: {}", body.err().unwrap());
true
}
} else {
eprintln!("Error: {}", res.err().unwrap());
true
}
} else { } else {
eprintln!("Error: {}", body.errorcode); eprintln!("Error: {}", req.err().unwrap());
true true
} }
}, },
@ -89,20 +110,35 @@ pub async fn isync_get() -> bool {
let req = client.get("https://ipass.ipost.rocks/saved") let req = client.get("https://ipass.ipost.rocks/saved")
.header("ipass-auth-token", token) .header("ipass-auth-token", token)
.timeout(std::time::Duration::from_secs(3)) .timeout(std::time::Duration::from_secs(3))
.build().unwrap(); .build();
let res = client.execute(req).await.unwrap(); if let Ok(req) = req {
let body = res.json::<Saved>().await.unwrap(); let res = client.execute(req).await;
if body.success { if let Ok(res) = res {
println!("new hash: {}",sha256hexhash(body.data.clone())); let body = res.json::<Saved>().await;
File::create(get_ipass_folder()+"temp.ipassx").unwrap().write_all(&body.data).unwrap(); if let Ok(body) = body {
import_file(&(get_ipass_folder()+"temp.ipassx")); if body.success {
std::fs::remove_file(get_ipass_folder()+"temp.ipassx").unwrap(); println!("new hash: {}",sha256hexhash(body.data.clone()));
return true; File::create(get_ipass_folder()+"temp.ipassx").unwrap().write_all(&body.data).unwrap();
} else { import_file(&(get_ipass_folder()+"temp.ipassx"));
if body.status == 200 { std::fs::remove_file(get_ipass_folder()+"temp.ipassx").unwrap();
return true; return true;
} else {
if body.status == 200 {
return true;
}
eprintln!("Error: {}", body.errorcode);
return false;
}
} else {
eprintln!("Error: {}", body.err().unwrap());
return false;
}
} else {
eprintln!("Error: {}", res.err().unwrap());
return false;
} }
eprintln!("Error: {}", body.errorcode); } else {
eprintln!("Error: {}", req.err().unwrap());
return false; return false;
} }
}, },