Skip to content

Commit

Permalink
Display Bad HTTP response in payjoin cli
Browse files Browse the repository at this point in the history
Show response details including the body so that rejected key errors are
clear.
  • Loading branch information
DanGould committed Dec 27, 2023
1 parent e92d6a4 commit 9e43b90
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions payjoin-cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ impl App {
);
let (req, ctx) = enroller.extract_req()?;
log::debug!("Enrolling receiver");
let http = http_agent()?;
let ohttp_response = spawn_blocking(move || {
let http = http_agent()?;
http.post(req.url.as_ref())
.send_bytes(&req.body)
.with_context(|| "HTTP request failed")
http.post(req.url.as_ref()).send_bytes(&req.body).map_err(map_ureq_err)
})
.await??;

Expand Down Expand Up @@ -172,7 +170,7 @@ impl App {
.extract_v2_req()
.map_err(|e| anyhow!("v2 req extraction failed {}", e))?;
let http = http_agent()?;
let res = http.post(req.url.as_str()).send_bytes(&req.body)?;
let res = http.post(req.url.as_str()).send_bytes(&req.body).map_err(map_ureq_err)?;
let mut buf = Vec::new();
let _ = res.into_reader().read_to_end(&mut buf)?;
let res = payjoin_proposal.deserialize_res(buf, ohttp_ctx);
Expand Down Expand Up @@ -204,7 +202,7 @@ impl App {
http.post(req.url.as_ref())
.set("Content-Type", "text/plain")
.send_bytes(&req.body)
.with_context(|| "HTTP request failed")
.map_err(map_ureq_err)
})
.await??;

Expand All @@ -229,8 +227,10 @@ impl App {
enrolled.extract_req().map_err(|_| anyhow!("Failed to extract request"))?;
log::debug!("GET fallback_psbt");
let http = http_agent()?;
let ohttp_response =
spawn_blocking(move || http.post(req.url.as_str()).send_bytes(&req.body)).await??;
let ohttp_response = spawn_blocking(move || {
http.post(req.url.as_str()).send_bytes(&req.body).map_err(map_ureq_err)
})
.await??;

let proposal = enrolled
.process_res(ohttp_response.into_reader(), context)
Expand Down Expand Up @@ -905,3 +905,15 @@ fn http_agent() -> Result<ureq::Agent> {

#[cfg(not(feature = "danger-local-https"))]
fn http_agent() -> Result<ureq::Agent> { Ok(ureq::Agent::new()) }

fn map_ureq_err(e: ureq::Error) -> anyhow::Error {
let e_string = e.to_string();
match e.into_response() {
Some(res) => anyhow!(
"HTTP request failed: {} {}",
res.status(),
res.into_string().unwrap_or_default()
),
None => anyhow!("No HTTP response: {}", e_string),
}
}

0 comments on commit 9e43b90

Please sign in to comment.