From b38ae5c99670d58b98f0adcb3b5efe79e1bab30a Mon Sep 17 00:00:00 2001 From: Adam Spofford Date: Thu, 3 Jul 2025 07:43:59 -0700 Subject: [PATCH] print error body for pocketic responses --- src/dfx/src/actors/pocketic.rs | 39 ++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/dfx/src/actors/pocketic.rs b/src/dfx/src/actors/pocketic.rs index eda76637bc..43d83fb2a7 100644 --- a/src/dfx/src/actors/pocketic.rs +++ b/src/dfx/src/actors/pocketic.rs @@ -25,6 +25,7 @@ use dfx_core::config::model::replica_config::CachedConfig; use dfx_core::config::model::replica_config::ReplicaConfig; #[cfg(unix)] use dfx_core::json::save_json_file; +use reqwest::Response; use slog::{debug, error, warn, Logger}; use std::net::SocketAddr; use std::ops::ControlFlow::{self, *}; @@ -415,7 +416,8 @@ async fn initialize_pocketic( }) .send() .await? - .error_for_status()? + .check_http_err() + .await? .json::() .await?; let instance = match resp { @@ -449,7 +451,8 @@ async fn initialize_pocketic( }) .send() .await? - .error_for_status()?; + .check_http_err() + .await?; init_client .post(format!( "http://localhost:{port}/instances/{instance}/auto_progress" @@ -459,7 +462,8 @@ async fn initialize_pocketic( }) .send() .await? - .error_for_status()?; + .check_http_err() + .await?; let agent_url = format!("http://localhost:{port}/instances/{instance}/"); @@ -518,7 +522,8 @@ async fn initialize_gateway( }) .send() .await? - .error_for_status()?; + .check_http_err() + .await?; let resp = resp.json::().await?; let instance = match resp { CreateHttpGatewayResponse::Created(info) => info.instance_id, @@ -556,17 +561,39 @@ async fn shutdown_pocketic( )) .send() .await? - .error_for_status()?; + .check_http_err() + .await?; shutdown_client .delete(format!( "http://localhost:{port}/instances/{server_instance}" )) .send() .await? - .error_for_status()?; + .check_http_err() + .await?; Ok(()) } +trait ResponseExt { + async fn check_http_err(self) -> DfxResult; +} + +impl ResponseExt for Response { + async fn check_http_err(self) -> DfxResult { + let status = self.status(); + if status.is_client_error() || status.is_server_error() { + bail!( + "{} {}: {}", + status.as_str(), + status.canonical_reason().unwrap(), + self.text().await? + ); + } else { + Ok(self) + } + } +} + #[cfg(not(unix))] fn shutdown_pocketic(_: u16, _: usize, _: usize, _: Logger) -> DfxResult { bail!("PocketIC not supported on this platform")