Skip to content

Commit

Permalink
fix(cli): fix consumed errors consumed by socket handler, fix empty b…
Browse files Browse the repository at this point in the history
…ody/plaintext error handling
  • Loading branch information
Meshiest committed Dec 10, 2024
1 parent 9186c83 commit 2847fa1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
18 changes: 16 additions & 2 deletions crates/cli/src/commands/env/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use clap::Parser;
use clap_stdin::FileOrStdin;
use reqwest::{Client, RequestBuilder, Response};
use serde_json::json;
use serde_json::{json, Value};
use snops_cli::events::EventsClient;
use snops_common::{
action_models::{AleoValue, WithTargets},
Expand Down Expand Up @@ -381,8 +381,22 @@ impl Action {

pub async fn post_and_wait_tx(url: &str, req: RequestBuilder) -> Result<()> {
use snops_common::events::EventFilter::*;
let res = req.send().await?;

if !res.status().is_success() {
eprintln!("error {}", res.status());
let value = match res.content_length() {
Some(0) | None => None,
_ => {
let text = res.text().await?;
Some(serde_json::from_str(&text).unwrap_or_else(|_| Value::String(text)))
}
};
println!("{}", serde_json::to_string_pretty(&value)?);
return Ok(());
}

let tx_id: String = req.send().await?.json().await?;
let tx_id: String = res.json().await?;
eprintln!("transaction id: {tx_id}");

let mut events = EventsClient::open_with_filter(url, TransactionIs(Arc::new(tx_id))).await?;
Expand Down
16 changes: 11 additions & 5 deletions crates/cli/src/commands/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::Result;
use clap::{Parser, ValueHint};
use clap_stdin::FileOrStdin;
use reqwest::{Client, RequestBuilder, Response};
use serde_json::Value;
use snops_cli::events::EventsClient;
use snops_common::{
action_models::AleoValue,
Expand Down Expand Up @@ -287,11 +288,16 @@ pub async fn post_and_wait(url: &str, req: RequestBuilder, env_id: EnvId) -> Res
let res = req.send().await?;

if !res.status().is_success() {
println!(
"{}",
serde_json::to_string_pretty(&res.json::<serde_json::Value>().await?)?
);
std::process::exit(1);
eprintln!("error: {}", res.status());
let value = match res.content_length() {
Some(0) | None => None,
_ => {
let text = res.text().await?;
Some(serde_json::from_str(&text).unwrap_or_else(|_| Value::String(text)))
}
};
println!("{}", serde_json::to_string_pretty(&value)?);
return Ok(());
}

let mut node_map: HashMap<NodeKey, AgentId> = res.json().await?;
Expand Down
5 changes: 4 additions & 1 deletion crates/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ impl Commands {

let value = match response.content_length() {
Some(0) | None => None,
_ => response.json::<Value>().await.map(Some)?,
_ => {
let text = response.text().await?;
Some(serde_json::from_str(&text).unwrap_or_else(|_| Value::String(text)))
}
};

println!("{}", serde_json::to_string_pretty(&value)?);
Expand Down

0 comments on commit 2847fa1

Please sign in to comment.