From 1cccac458c07d69be1537c9f64fa0134605df2eb Mon Sep 17 00:00:00 2001 From: Tyr Chen Date: Wed, 31 Aug 2022 15:07:31 -0700 Subject: [PATCH] better error output --- Cargo.lock | 6 +++--- requester/Cargo.toml | 2 +- requester/src/diff.rs | 15 ++++++++------- requester/src/req.rs | 19 +++++++++++-------- xdiff/Cargo.toml | 4 ++-- xreq/Cargo.toml | 4 ++-- xreq/src/main.rs | 1 + 7 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c0b38f2..4e2c52f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1591,7 +1591,7 @@ dependencies = [ [[package]] name = "xdiff" -version = "0.4.2" +version = "0.4.3" dependencies = [ "anyhow", "atty", @@ -1612,7 +1612,7 @@ checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" [[package]] name = "xreq" -version = "0.4.2" +version = "0.4.3" dependencies = [ "anyhow", "atty", @@ -1639,7 +1639,7 @@ dependencies = [ [[package]] name = "xreq-lib" -version = "0.4.0" +version = "0.4.1" dependencies = [ "anyhow", "console", diff --git a/requester/Cargo.toml b/requester/Cargo.toml index 4e506d5..0f027cb 100644 --- a/requester/Cargo.toml +++ b/requester/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xreq-lib" -version = "0.4.0" +version = "0.4.1" edition = "2021" license = "MIT" documentation = "https://docs.rs/xreq-lib" diff --git a/requester/src/diff.rs b/requester/src/diff.rs index 2281ac1..e610ae0 100644 --- a/requester/src/diff.rs +++ b/requester/src/diff.rs @@ -83,16 +83,17 @@ impl DiffConfig { } pub fn get(&self, profile: &str) -> Result<&DiffContext> { - self.ctxs - .get(profile) - .ok_or_else(|| anyhow::anyhow!("profile {} not found", profile)) + self.ctxs.get(profile).ok_or_else(|| { + anyhow::anyhow!( + "profile {} not found. Available profiles: {:?}.", + profile, + self.ctxs.keys() + ) + }) } pub async fn diff(&self, profile: &str) -> Result { - let ctx = self - .ctxs - .get(profile) - .ok_or_else(|| anyhow::anyhow!("profile {} not found", profile))?; + let ctx = self.get(profile)?; ctx.diff().await } diff --git a/requester/src/req.rs b/requester/src/req.rs index 81a9519..7c19b71 100644 --- a/requester/src/req.rs +++ b/requester/src/req.rs @@ -72,16 +72,17 @@ impl RequestConfig { } pub fn get(&self, profile: &str) -> Result<&RequestContext> { - self.ctxs - .get(profile) - .ok_or_else(|| anyhow::anyhow!("profile {} not found", profile)) + self.ctxs.get(profile).ok_or_else(|| { + anyhow::anyhow!( + "profile {} not found. Available profiles: {:?}.", + profile, + self.ctxs.keys() + ) + }) } pub async fn send(&self, profile: &str) -> Result { - let ctx = self - .ctxs - .get(profile) - .ok_or_else(|| anyhow::anyhow!("profile {} not found", profile))?; + let ctx = self.get(profile)?; ctx.send().await } @@ -120,7 +121,9 @@ impl RequestContext { match url.scheme() { "http" | "https" => { let qs = serde_qs::to_string(&self.params)?; - url.set_query(Some(&qs)); + if !qs.is_empty() { + url.set_query(Some(&qs)); + } let client = Client::builder().user_agent(user_agent).build()?; let mut builder = client diff --git a/xdiff/Cargo.toml b/xdiff/Cargo.toml index c2953ca..f3c60d5 100644 --- a/xdiff/Cargo.toml +++ b/xdiff/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xdiff" -version = "0.4.2" +version = "0.4.3" edition = "2021" license = "MIT" documentation = "https://docs.rs/xdiff" @@ -23,4 +23,4 @@ serde_json = "1.0.85" serde_yaml = "0.9.10" xreq-cli-utils = { version = "0.3.1", path = "../cli-utils" } -xreq-lib = { version = "0.4.0", path = "../requester" } +xreq-lib = { version = "0.4.1", path = "../requester" } diff --git a/xreq/Cargo.toml b/xreq/Cargo.toml index 982a5c0..e65ae65 100644 --- a/xreq/Cargo.toml +++ b/xreq/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xreq" -version = "0.4.2" +version = "0.4.3" edition = "2021" license = "MIT" documentation = "https://docs.rs/xreq" @@ -25,4 +25,4 @@ serde_yaml = "0.9.10" tokio = { version = "1.20.1", features = ["full"] } xreq-cli-utils = { version = "0.3.1", path = "../cli-utils" } -xreq-lib = { version = "0.4.0", path = "../requester" } +xreq-lib = { version = "0.4.1", path = "../requester" } diff --git a/xreq/src/main.rs b/xreq/src/main.rs index f1c8f87..bb41192 100644 --- a/xreq/src/main.rs +++ b/xreq/src/main.rs @@ -110,6 +110,7 @@ async fn run(output: &mut Vec, args: RunArgs) -> Result<()> { let resp = config.send().await?; if atty::is(atty::Stream::Stdout) { + output.push(format!("Sending: {}\n\n", resp.url())); print_status(output, &resp); print_headers(output, &resp); }