Skip to content

Commit 028f205

Browse files
authored
fix: preserve response body when error_status_codes triggers (#668)
Previously, when AWS_LWA_ERROR_STATUS_CODES matched a response status code, the adapter discarded the HTTP response body and returned a generic error message. This broke Step Functions workflows that rely on error payloads for branching and retry logic. Now the error includes a JSON object with both the status code and the original response body: {"statusCode":502,"body":"Bad Gateway"} This allows consumers like Step Functions to inspect the original error details for decision making. Fixes #609
1 parent fb9e768 commit 028f205

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ exclude = ["examples"]
2020
bytes = "1.9.0"
2121
http = "1.2.0"
2222
http-body = "1.0.1"
23+
http-body-util = "0.1.0"
2324
hyper = { version = "1.5.2", features = ["client"] }
2425
hyper-util = "0.1.10"
2526
lambda_http = { version = "1.1.1", default-features = false, features = [
@@ -51,7 +52,6 @@ url = "2.5.4"
5152
[dev-dependencies]
5253
flate2 = "1.0.25"
5354
httpmock = "0.8.2"
54-
http-body-util = "0.1.0"
5555
http-body = "1.0"
5656
hyper-rustls = "0.27"
5757
aws-sigv4 = "1.2.3"

src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ use http::{
9797
Method, StatusCode,
9898
};
9999
use http_body::Body as HttpBody;
100+
use http_body_util::BodyExt;
100101
use hyper::body::Incoming;
101102
use hyper_util::client::legacy::connect::HttpConnector;
102103
use hyper_util::client::legacy::Client;
@@ -952,9 +953,17 @@ impl Adapter<HttpConnector, Body> {
952953
if let Some(error_codes) = &self.error_status_codes {
953954
let status = app_response.status().as_u16();
954955
if error_codes.contains(&status) {
956+
let body_bytes = app_response
957+
.into_body()
958+
.collect()
959+
.await
960+
.map(|c| c.to_bytes())
961+
.unwrap_or_default();
962+
let body_str = String::from_utf8_lossy(&body_bytes);
955963
return Err(Error::from(format!(
956-
"Request failed with configured error status code: {}",
957-
status
964+
"{{\"statusCode\":{},\"body\":{}}}",
965+
status,
966+
serde_json::to_string(&*body_str).unwrap_or_else(|_| format!("\"{}\"", body_str))
958967
)));
959968
}
960969
}

tests/integ_tests/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,9 @@ async fn test_http_error_status_codes() {
740740

741741
let result = adapter.call(request).await;
742742
assert!(result.is_err(), "Expected error response for status code 502");
743-
assert!(result.unwrap_err().to_string().contains("502"));
743+
let err_msg = result.unwrap_err().to_string();
744+
assert!(err_msg.contains("502"), "Error should contain status code");
745+
assert!(err_msg.contains("Bad Gateway"), "Error should preserve response body");
744746

745747
// Assert endpoint was called
746748
error_endpoint.assert();

0 commit comments

Comments
 (0)