refactor(proto): replace anyhow with thiserror in readers
This commit is contained in:
parent
58c69928a1
commit
da6410ce05
5 changed files with 80 additions and 23 deletions
|
|
@ -3,13 +3,14 @@ use crate::reader::protoreader::ProtoReader;
|
|||
use crate::reader::utils::AsyncPeek;
|
||||
use async_trait::async_trait;
|
||||
use tokio::io::{AsyncBufRead, AsyncReadExt};
|
||||
use crate::reader::errors::ProtoReadError;
|
||||
|
||||
#[async_trait]
|
||||
pub trait OneWayProtoReader<T>
|
||||
where
|
||||
T: ProtoMessage,
|
||||
{
|
||||
async fn read_proto(&mut self) -> anyhow::Result<T>;
|
||||
async fn read_proto(&mut self) -> Result<T, ProtoReadError>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
|
@ -18,15 +19,18 @@ where
|
|||
R: AsyncBufRead + AsyncPeek + Unpin + Send,
|
||||
T: ProtoMessage,
|
||||
{
|
||||
async fn read_proto(&mut self) -> anyhow::Result<T> {
|
||||
async fn read_proto(&mut self) -> Result<T, ProtoReadError> {
|
||||
let variant = self.inner.read_u8().await?;
|
||||
let length = self.inner.read_i32().await?;
|
||||
|
||||
if length < 4 {
|
||||
return Err(anyhow::anyhow!("Invalid message length"));
|
||||
return Err(ProtoReadError::InvalidLength(length));
|
||||
}
|
||||
if length > self.msg_len_limit {
|
||||
return Err(anyhow::anyhow!("Message length over limit"));
|
||||
return Err(ProtoReadError::LengthOverflow {
|
||||
limit: self.msg_len_limit as usize,
|
||||
actual: length as usize,
|
||||
});
|
||||
}
|
||||
|
||||
let mut data = vec![0u8; (length - 4) as usize];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue