Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ exclude = ["examples"]
bytes = "1.9.0"
http = "1.2.0"
http-body = "1.0.1"
http-body-util = "0.1.0"
hyper = { version = "1.5.2", features = ["client"] }
hyper-util = "0.1.10"
lambda_http = { version = "1.1.1", default-features = false, features = [
Expand Down Expand Up @@ -51,7 +52,6 @@ url = "2.5.4"
[dev-dependencies]
flate2 = "1.0.25"
httpmock = "0.8.2"
http-body-util = "0.1.0"
http-body = "1.0"
hyper-rustls = "0.27"
aws-sigv4 = "1.2.3"
Expand Down
13 changes: 11 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ use http::{
Method, StatusCode,
};
use http_body::Body as HttpBody;
use http_body_util::BodyExt;
use hyper::body::Incoming;
use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::client::legacy::Client;
Expand Down Expand Up @@ -952,9 +953,17 @@ impl Adapter<HttpConnector, Body> {
if let Some(error_codes) = &self.error_status_codes {
let status = app_response.status().as_u16();
if error_codes.contains(&status) {
let body_bytes = app_response
.into_body()
.collect()
.await
.map(|c| c.to_bytes())
.unwrap_or_default();
let body_str = String::from_utf8_lossy(&body_bytes);
return Err(Error::from(format!(
"Request failed with configured error status code: {}",
status
"{{\"statusCode\":{},\"body\":{}}}",
status,
serde_json::to_string(&*body_str).unwrap_or_else(|_| format!("\"{}\"", body_str))
)));
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/integ_tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,9 @@ async fn test_http_error_status_codes() {

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

// Assert endpoint was called
error_endpoint.assert();
Expand Down
Loading