Improve error handling by using decode::Error instead of io::Result (#2078)

Use decode::Error instead of io::Result
This commit is contained in:
Julian Braha 2024-02-20 20:06:05 +00:00 committed by GitHub
parent 84dadffa81
commit af8dbf7789
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,3 +1,4 @@
use sd_p2p::proto::decode;
use tokio::io::{AsyncRead, AsyncReadExt};
// will probs have more variants in future
@ -8,14 +9,13 @@ pub enum SyncMessage {
impl SyncMessage {
// TODO: Per field errors for better error handling
// TODO: Using `decode::Error` instead of `io::Result`
pub async fn from_stream(stream: &mut (impl AsyncRead + Unpin)) -> std::io::Result<Self> {
pub async fn from_stream(stream: &mut (impl AsyncRead + Unpin)) -> Result<Self, decode::Error> {
match stream.read_u8().await? {
b'N' => Ok(Self::NewOperations),
header => Err(std::io::Error::new(
header => Err(decode::Error::IoError(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Invalid sync message header: {}", (header as char)),
)),
))),
}
}