From 4ef48b897a10bcbbd9b3db9d57304797f513ea39 Mon Sep 17 00:00:00 2001 From: Sai Harsha Vardhan <56996463+sai-harsha-vardhan@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:37:07 +0530 Subject: [PATCH] fix(router): handle empty body parse failures in bad request logger middleware (#3541) --- crates/router/src/lib.rs | 2 +- crates/router/src/middleware.rs | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/crates/router/src/lib.rs b/crates/router/src/lib.rs index bb56a173da37..2d2526645ea4 100644 --- a/crates/router/src/lib.rs +++ b/crates/router/src/lib.rs @@ -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()) } diff --git a/crates/router/src/middleware.rs b/crates/router/src/middleware.rs index 9205f53a04b7..6f3427bf7a4b 100644 --- a/crates/router/src/middleware.rs +++ b/crates/router/src/middleware.rs @@ -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::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) })