Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate error handling to anyhow #26

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = "0.11"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread"] }
uuid = "1.2"
sha2 = "0.10"
url = "2"

env_logger = "0.10"
log = "0.4"
anyhow = "1.0.75"
argh = "0.1"
bzip2 = "0.4.4"
env_logger = "0.10"
globset = "0.4"
log = "0.4"
protobuf = "3.2.0"
bzip2 = "0.4.4"
reqwest = "0.11"
sha2 = "0.10"
tempfile = "3.8.1"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread"] }
url = "2"
uuid = "1.2"

[dependencies.hard-xml]
path = "vendor/hard-xml"
Expand Down
23 changes: 15 additions & 8 deletions examples/full_test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::error::Error;
use std::borrow::Cow;

use anyhow::{Context, Result};
use hard_xml::XmlRead;
use url::Url;

fn get_pkgs_to_download(resp: &omaha::Response) -> Result<Vec<(Url, omaha::Hash<omaha::Sha256>)>, Box<dyn Error>> {
fn get_pkgs_to_download(resp: &omaha::Response) -> Result<Vec<(Url, omaha::Hash<omaha::Sha256>)>> {
let mut to_download: Vec<(Url, omaha::Hash<_>)> = Vec::new();

for app in &resp.apps {
Expand Down Expand Up @@ -44,27 +45,33 @@ fn get_pkgs_to_download(resp: &omaha::Response) -> Result<Vec<(Url, omaha::Hash<
async fn main() -> Result<(), Box<dyn Error>> {
let client = reqwest::Client::new();

const APP_VERSION_DEFAULT: &str = "3340.0.0+nightly-20220823-2100";
const MACHINE_ID_DEFAULT: &str = "abce671d61774703ac7be60715220bfe";
const TRACK_DEFAULT: &str = "stable";

////
// request
////
let parameters = ue_rs::request::Parameters {
app_version: Cow::Borrowed("3340.0.0+nightly-20220823-2100"),
machine_id: Cow::Borrowed("abce671d61774703ac7be60715220bfe"),
app_version: Cow::Borrowed(APP_VERSION_DEFAULT),
machine_id: Cow::Borrowed(MACHINE_ID_DEFAULT),

track: Cow::Borrowed("stable"),
track: Cow::Borrowed(TRACK_DEFAULT),
};

let response_text = ue_rs::request::perform(&client, parameters).await?;
let response_text = ue_rs::request::perform(&client, parameters).await.context(format!(
"perform({APP_VERSION_DEFAULT}, {MACHINE_ID_DEFAULT}, {TRACK_DEFAULT}) failed"
))?;

println!("response:\n\t{:#?}", response_text);
println!();

////
// parse response
////
let resp = omaha::Response::from_str(&response_text)?;
let resp = omaha::Response::from_str(&response_text).context("failed to parse response")?;

let pkgs_to_dl = get_pkgs_to_download(&resp)?;
let pkgs_to_dl = get_pkgs_to_download(&resp).context("failed to get packages to download")?;

////
// download
Expand All @@ -76,7 +83,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// std::io::BufWriter wrapping an std::fs::File is probably the right choice.
// std::io::sink() is basically just /dev/null
let data = std::io::sink();
let res = ue_rs::download_and_hash(&client, url, data).await?;
let res = ue_rs::download_and_hash(&client, url.clone(), data).await.context(format!("download_and_hash({url:?}) failed"))?;

println!("\texpected sha256: {}", expected_sha256);
println!("\tcalculated sha256: {}", res.hash);
Expand Down
16 changes: 12 additions & 4 deletions examples/request.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
use std::error::Error;
use std::borrow::Cow;

use anyhow::Context;

use ue_rs::request;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = reqwest::Client::new();

const APP_VERSION_DEFAULT: &str = "3340.0.0+nightly-20220823-2100";
const MACHINE_ID_DEFAULT: &str = "abce671d61774703ac7be60715220bfe";
const TRACK_DEFAULT: &str = "stable";

let parameters = request::Parameters {
app_version: Cow::Borrowed("3340.0.0+nightly-20220823-2100"),
machine_id: Cow::Borrowed("abce671d61774703ac7be60715220bfe"),
app_version: Cow::Borrowed(APP_VERSION_DEFAULT),
machine_id: Cow::Borrowed(MACHINE_ID_DEFAULT),

track: Cow::Borrowed("stable"),
track: Cow::Borrowed(TRACK_DEFAULT),
};

let response = request::perform(&client, parameters).await?;
let response = request::perform(&client, parameters).await.context(format!(
"perform({APP_VERSION_DEFAULT}, {MACHINE_ID_DEFAULT}, {TRACK_DEFAULT}) failed"
))?;

println!("response:\n\t{:#?}", response);

Expand Down
8 changes: 6 additions & 2 deletions examples/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::error::Error;

use anyhow::Context;
use hard_xml::XmlRead;
use omaha;

Expand Down Expand Up @@ -29,7 +30,7 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("{}", RESPONSE_XML);
println!();

let resp = omaha::Response::from_str(RESPONSE_XML)?;
let resp = omaha::Response::from_str(RESPONSE_XML).context("failed to create response")?;

println!("{:#?}", resp);
println!();
Expand Down Expand Up @@ -66,7 +67,10 @@ fn main() -> Result<(), Box<dyn Error>> {
println!(" urls:");

for url in &app.update_check.urls {
println!(" {}", url.join(&pkg.name)?);
println!(
" {}",
url.join(&pkg.name).context(format!("failed to join URL with {:?}", pkg.name))?
);
}

println!();
Expand Down
1 change: 1 addition & 0 deletions omaha/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
uuid = "1.2"
ct-codecs = "1"
url = "2"
anyhow = "1.0.75"

[dependencies.hard-xml]
path = "../vendor/hard-xml"
8 changes: 4 additions & 4 deletions omaha/src/hash_types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::fmt;
use std::str;

use anyhow::{Error as CodecError, anyhow};

#[rustfmt::skip]
use ct_codecs::{
Error as CodecError,

Base64,
Hex,

Expand Down Expand Up @@ -82,9 +82,9 @@ impl<T: HashAlgo> Into<Vec<u8>> for Hash<T> {

impl<T: HashAlgo> Hash<T> {
#[inline]
fn decode<D: Decoder>(hash: &str) -> Result<Self, CodecError> {
fn decode<D: Decoder>(hash: &str) -> anyhow::Result<Self, CodecError> {
let mut digest = T::Output::default();
D::decode(digest.as_mut(), hash, None)?;
D::decode(digest.as_mut(), hash, None).map_err(|_| anyhow!("decode ({}) failed", hash))?;
Ok(Self(digest))
}

Expand Down
4 changes: 2 additions & 2 deletions omaha/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use self::omaha::{Sha1, Sha256};
mod sha256_hex {
use crate as omaha;
use self::omaha::Sha256;
use ct_codecs::Error;
use anyhow::Error as CodecError;

#[inline]
pub(crate) fn from_str(s: &str) -> Result<omaha::Hash<Sha256>, Error> {
pub(crate) fn from_str(s: &str) -> Result<omaha::Hash<Sha256>, CodecError> {
<omaha::Hash<Sha256>>::from_hex(s)
}
}
Expand Down
Loading
Loading