From fb2ff892322d32f7e47e61effb805579ad4593b8 Mon Sep 17 00:00:00 2001 From: David Calavera Date: Sun, 9 Apr 2023 23:44:36 -0700 Subject: [PATCH] Add debug assertions check to streaming This is used by Cargo Lambda's local server to control req/response. Signed-off-by: David Calavera --- lambda-runtime/src/streaming.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lambda-runtime/src/streaming.rs b/lambda-runtime/src/streaming.rs index 85af784e..15cd210f 100644 --- a/lambda-runtime/src/streaming.rs +++ b/lambda-runtime/src/streaming.rs @@ -108,6 +108,14 @@ where let event = next_event_response?; let (parts, body) = event.into_parts(); + #[cfg(debug_assertions)] + if parts.status == http::StatusCode::NO_CONTENT { + // Ignore the event if the status code is 204. + // This is a way to keep the runtime alive when + // there are no events pending to be processed. + continue; + } + let ctx: Context = Context::try_from(parts.headers)?; let ctx: Context = ctx.with_config(config); let request_id = &ctx.request_id.clone(); @@ -128,6 +136,12 @@ where let body = hyper::body::to_bytes(body).await?; trace!("incoming request payload - {}", std::str::from_utf8(&body)?); + #[cfg(debug_assertions)] + if parts.status.is_server_error() { + error!("Lambda Runtime server returned an unexpected error"); + return Err(parts.status.to_string().into()); + } + let body = match serde_json::from_slice(&body) { Ok(body) => body, Err(err) => {