From 40b49543f14ad0f26b03e112f0fc145c5d574c32 Mon Sep 17 00:00:00 2001 From: Mubelotix Date: Tue, 26 Aug 2025 12:11:03 +0200 Subject: [PATCH] Improve error detection --- crates/xtask/src/common/command.rs | 49 ++++++++++++++++-------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/crates/xtask/src/common/command.rs b/crates/xtask/src/common/command.rs index c08ea949a..47ff3bab6 100644 --- a/crates/xtask/src/common/command.rs +++ b/crates/xtask/src/common/command.rs @@ -185,6 +185,32 @@ pub async fn run( request.send().await.with_context(|| format!("error sending command: {}", command))?; let code = response.status(); + + if let Some(expected_response) = &command.expected_response { + let response: serde_json::Value = response + .json() + .await + .context("could not deserialize response as JSON") + .context("parsing response when checking expected response")?; + if return_value { + return Ok(Some((response, code))); + } + if &response != expected_response { + let expected_pretty = serde_json::to_string_pretty(expected_response) + .context("serializing expected response as pretty JSON")?; + let response_pretty = serde_json::to_string_pretty(&response) + .context("serializing response as pretty JSON")?; + let diff = SimpleDiff::from_str(&expected_pretty, &response_pretty, "expected", "got"); + bail!("unexpected response:\n{diff}"); + } + } else if return_value { + let response: serde_json::Value = response + .json() + .await + .context("could not deserialize response as JSON") + .context("parsing response when recording expected response")?; + return Ok(Some((response, code))); + } if let Some(expected_status) = command.expected_status { if code.as_u16() != expected_status { @@ -213,29 +239,6 @@ pub async fn run( bail!("server error: server responded with error code {code} and '{response}'") } - if return_value { - let response: serde_json::Value = response - .json() - .await - .context("could not deserialize response as JSON") - .context("parsing response when recording expected response")?; - return Ok(Some((response, code))); - } else if let Some(expected_response) = &command.expected_response { - let response: serde_json::Value = response - .json() - .await - .context("could not deserialize response as JSON") - .context("parsing response when checking expected response")?; - if &response != expected_response { - let expected_pretty = serde_json::to_string_pretty(expected_response) - .context("serializing expected response as pretty JSON")?; - let response_pretty = serde_json::to_string_pretty(&response) - .context("serializing response as pretty JSON")?; - let diff = SimpleDiff::from_str(&expected_pretty, &response_pretty, "expected", "got"); - bail!("unexpected response:\n{diff}"); - } - } - Ok(None) }