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.
This commit is contained in:
@@ -41,11 +41,11 @@ impl Protocol300Transmission {
|
|||||||
[0x15] => Ok(Self::Nack),
|
[0x15] => Ok(Self::Nack),
|
||||||
[0x16, 0x00, 0x00] => Ok(Self::Init),
|
[0x16, 0x00, 0x00] => Ok(Self::Init),
|
||||||
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes)?)),
|
[0x41, ..] => Ok(Self::Message(Protocol300Message::try_from_bytes(bytes)?)),
|
||||||
_ => {
|
b => {
|
||||||
error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})",
|
error!("Failed to parse Protocol300 transmission: invalid byte sequence (length: {}, first byte: 0x{:02X})",
|
||||||
bytes.len(),
|
bytes.len(),
|
||||||
bytes.first().unwrap_or(&0x00));
|
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
|
/// Errors that can occur when getting a Protocol 300 Transmission from bytes
|
||||||
#[derive(Error, Debug, PartialEq, Eq)]
|
#[derive(Error, Debug, PartialEq, Eq)]
|
||||||
pub enum FromBytesError{
|
pub enum FromBytesError<'a>{
|
||||||
#[error("Failed to parse message")]
|
#[error("Failed to parse message")]
|
||||||
MessageParsing(#[from] message::MessageDecodingError),
|
MessageParsing(#[from] message::MessageDecodingError),
|
||||||
#[error("Invalid bytes in vec")]
|
#[error("Invalid bytes in vec")]
|
||||||
InvalidByte
|
InvalidBytes(&'a [u8])
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user