diff --git a/crates/cli/src/commands/env/action/mod.rs b/crates/cli/src/commands/env/action/mod.rs index 9050cdd6..1a2dd779 100644 --- a/crates/cli/src/commands/env/action/mod.rs +++ b/crates/cli/src/commands/env/action/mod.rs @@ -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}, @@ -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?; diff --git a/crates/cli/src/commands/env/mod.rs b/crates/cli/src/commands/env/mod.rs index 275971eb..bc6951d4 100644 --- a/crates/cli/src/commands/env/mod.rs +++ b/crates/cli/src/commands/env/mod.rs @@ -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, @@ -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::().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 = res.json().await?; diff --git a/crates/cli/src/commands/mod.rs b/crates/cli/src/commands/mod.rs index 4f5eaab1..79f9a2ef 100644 --- a/crates/cli/src/commands/mod.rs +++ b/crates/cli/src/commands/mod.rs @@ -90,7 +90,10 @@ impl Commands { let value = match response.content_length() { Some(0) | None => None, - _ => response.json::().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)?);