From 37682904c5d74057ae3ad8dc72cf6013110d5e53 Mon Sep 17 00:00:00 2001 From: Rob Date: Mon, 10 Jun 2024 14:50:18 -0400 Subject: [PATCH] better errors --- cdn-proto/src/connection/protocols/mod.rs | 28 +++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/cdn-proto/src/connection/protocols/mod.rs b/cdn-proto/src/connection/protocols/mod.rs index 4d46aca..5023768 100644 --- a/cdn-proto/src/connection/protocols/mod.rs +++ b/cdn-proto/src/connection/protocols/mod.rs @@ -11,6 +11,7 @@ use tokio::{ task::AbortHandle, time::timeout, }; +use tracing::warn; use super::{middleware::Middleware, Bytes}; use crate::{ @@ -142,14 +143,16 @@ impl Connection { match message { BytesOrSoftClose::Bytes(message) => { // Write the message to the stream - if write_length_delimited(&mut writer, message).await.is_err() { + if let Err(err) = write_length_delimited(&mut writer, message).await { + warn!("failed to write message to stream: {:?}", err); receive_from_caller.close(); return; }; // Flush the writer // Is a no-op for everything but TCP+TLS - if writer.flush().await.is_err() { + if let Err(err) = writer.flush().await { + warn!("failed to flush writer: {:?}", err); receive_from_caller.close(); return; }; @@ -169,11 +172,22 @@ impl Connection { // Spawn the task that receives from the stream and sends to the caller let receiver_task = tokio::spawn(async move { // While we can successfully read messages from the stream, - while let Ok(message) = read_length_delimited::(&mut reader, &middleware).await { - if send_to_caller.send(message).await.is_err() { - send_to_caller.close(); - return; - }; + loop { + // Read the message from the stream + match read_length_delimited::(&mut reader, &middleware).await { + Ok(message) => { + // If successful, send the message to the caller + if send_to_caller.send(message).await.is_err() { + send_to_caller.close(); + return; + }; + } + Err(err) => { + // If we fail to read the message, log the error and break + warn!("failed to read message from stream: {:?}", err); + break; + } + } } }) .abort_handle();