Skip to content

Commit

Permalink
fix(router): handle empty body parse failures in bad request logger m…
Browse files Browse the repository at this point in the history
…iddleware (#3541)
  • Loading branch information
sai-harsha-vardhan committed Feb 5, 2024
1 parent b2f4e61 commit 4ef48b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ pub fn get_application_builder(
.wrap(middleware::default_response_headers())
.wrap(middleware::RequestId)
.wrap(cors::cors())
.wrap(middleware::LogSpanInitializer)
// this middleware works only for Http1.1 requests
.wrap(middleware::Http400RequestDetailsLogger)
.wrap(middleware::LogSpanInitializer)
.wrap(router_env::tracing_actix_web::TracingLogger::default())
}
23 changes: 17 additions & 6 deletions crates/router/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,24 @@ where
let response = response_fut.await?;
// Log the request_details when we receive 400 status from the application
if response.status() == 400 {
let value: serde_json::Value = serde_json::from_slice(&bytes)?;
let request_id = request_id_fut.await?.as_hyphenated().to_string();
logger::info!(
"request_id: {}, request_details: {}",
request_id,
get_request_details_from_value(&value, "")
);
if !bytes.is_empty() {
let value_result: Result<serde_json::Value, serde_json::Error> =
serde_json::from_slice(&bytes);
match value_result {
Ok(value) => {
logger::info!(
"request_id: {request_id}, request_details: {}",
get_request_details_from_value(&value, "")
);
}
Err(err) => {
logger::warn!("error while parsing the request in json value: {err}");
}
}
} else {
logger::info!("request_id: {request_id}, request_details: Empty Body");
}
}
Ok(response)
})
Expand Down

0 comments on commit 4ef48b8

Please sign in to comment.