From 9072ae01ada57b86348f7598a126c794486ff5ed Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Sun, 18 Jan 2026 19:16:03 +0100 Subject: [PATCH] feat(transmission): improve error handling for invalid byte sequences Refactored Protocol300Transmission parsing to provide more detailed error information when encountering invalid byte sequences. Changed the error variant from `InvalidByte` to `InvalidBytes` which now includes the problematic byte slice, enabling better debugging. Updated the match arm to capture the byte slice as `b` instead of using a wildcard, and modified the error logging to reflect these changes. The lifetime parameter was added to the `FromBytesError` enum to support the byte slice reference. --- src/transmission.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transmission.rs b/src/transmission.rs index 4295092..d07b517 100644 --- a/src/transmission.rs +++ b/src/transmission.rs @@ -41,11 +41,11 @@ impl Protocol300Transmission { [0x15] => Ok(Self::Nack), [0x16, 0x00, 0x00] => Ok(Self::Init), [0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes)?)), - _ => { + b => { error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})", bytes.len(), bytes.first().unwrap_or(&0x00)); - Err(FromBytesError::InvalidByte) + Err(FromBytesError::InvalidBytes(b)) }, } } @@ -75,11 +75,11 @@ impl Protocol300Transmission { /// Errors that can occur when getting a Protocol 300 Transmission from bytes #[derive(Error, Debug, PartialEq, Eq)] -pub enum FromBytesError{ +pub enum FromBytesError<'a>{ #[error("Failed to parse message")] MessageParsing(#[from] message::MessageDecodingError), #[error("Invalid bytes in vec")] - InvalidByte + InvalidBytes(&'a [u8]) }