use std::io;
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("invalid protocol version")]
InvalidVersion,
#[error("invalid msg type")]
InvalidMsgType,
#[error("session shutdown")]
SessionShutdown,
#[error("streams exhausted")]
StreamsExhausted,
#[error("duplicate stream initiated")]
DuplicateStream,
#[error("i/o deadline reached")]
Timeout,
#[error("stream closed")]
StreamClosed,
#[error("stream reset by peer")]
StreamReset,
#[error("connection write timeout")]
ConnectionWriteTimeout,
#[error("connection timeout")]
ConnectionTimeout,
#[error("keepalive timeout")]
KeepAliveTimeout,
#[error("end of stream")]
EndOfStream,
#[error("now the session is unhealthy, please retry later")]
SessionUnhealthy,
#[error("current buffer is not enough data to read")]
NotEnoughData,
#[error("share memory no more buffer")]
NoMoreBuffer,
#[error("alloc size exceed")]
SizeTooLarge,
#[error("share memory's buffer had broken")]
BrokenBuffer,
#[error("share memory had not left space")]
ShareMemoryHadNotLeftSpace,
#[error("stream callbacks had existed")]
StreamCallbackHadExisted,
#[error("exchange config protocol invalid")]
ExchangeConfig,
#[error("exchange config timeout")]
ExchangeConfigTimeout,
#[error("shmipc just support linux OS now")]
OSNonSupported,
#[error("shmipc just support amd64 or arm64 arch")]
ArchNonSupported,
#[error("hot restart in progress, try again later")]
HotRestartInProgress,
#[error("session in handshake stage, try again later")]
InHandshakeStage,
#[error("share memory path prefix too long")]
FileNameTooLong,
#[error("the queue is empty")]
QueueEmpty,
#[error("the queue is full")]
QueueFull,
#[error("stream pool is full")]
StreamPoolFull,
#[error("stream had unread data, size: {0}")]
StreamHasUnreadData(usize),
#[error("stream had pending data, pending slice len: {0}")]
StreamHasPendingData(usize),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Others(#[from] anyhow::Error),
}
impl From<Error> for io::Error {
fn from(value: Error) -> Self {
match value {
Error::InvalidVersion => io::Error::new(io::ErrorKind::InvalidData, value),
Error::InvalidMsgType => io::Error::new(io::ErrorKind::InvalidData, value),
Error::SessionShutdown => io::Error::new(io::ErrorKind::ConnectionReset, value),
Error::StreamsExhausted => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::DuplicateStream => io::Error::new(io::ErrorKind::AlreadyExists, value),
Error::Timeout => io::Error::new(io::ErrorKind::TimedOut, value),
Error::StreamClosed => io::Error::new(io::ErrorKind::ConnectionAborted, value),
Error::StreamReset => io::Error::new(io::ErrorKind::ConnectionReset, value),
Error::ConnectionWriteTimeout => io::Error::new(io::ErrorKind::InvalidData, value),
Error::ConnectionTimeout => io::Error::new(io::ErrorKind::TimedOut, value),
Error::KeepAliveTimeout => io::Error::new(io::ErrorKind::TimedOut, value),
Error::EndOfStream => io::Error::new(io::ErrorKind::UnexpectedEof, value),
Error::SessionUnhealthy => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::NotEnoughData => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::NoMoreBuffer => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::SizeTooLarge => io::Error::new(io::ErrorKind::OutOfMemory, value),
Error::BrokenBuffer => io::Error::new(io::ErrorKind::BrokenPipe, value),
Error::ShareMemoryHadNotLeftSpace => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::StreamCallbackHadExisted => io::Error::new(io::ErrorKind::AlreadyExists, value),
Error::ExchangeConfig => io::Error::other(value),
Error::ExchangeConfigTimeout => io::Error::new(io::ErrorKind::TimedOut, value),
Error::OSNonSupported => io::Error::new(io::ErrorKind::Unsupported, value),
Error::ArchNonSupported => io::Error::new(io::ErrorKind::Unsupported, value),
Error::HotRestartInProgress => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::InHandshakeStage => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::FileNameTooLong => io::Error::new(io::ErrorKind::InvalidInput, value),
Error::QueueEmpty => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::QueueFull => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::StreamPoolFull => io::Error::new(io::ErrorKind::ResourceBusy, value),
Error::StreamHasUnreadData(_) => io::Error::other(value),
Error::StreamHasPendingData(_) => io::Error::other(value),
Error::Io(io) => io,
Error::Others(err) => io::Error::other(err),
}
}
}