mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-06-09 08:07:43 +00:00
use thiserror to make error handler simple
This commit is contained in:
parent
bfa1bbc462
commit
197b1c83a4
2 changed files with 28 additions and 53 deletions
|
@ -13,6 +13,7 @@ hashlink = "0.8"
|
|||
log = "0.4"
|
||||
mio = { version = "0.8", features = ["os-poll", "net", "os-ext"] }
|
||||
smoltcp = { version = "0.9", git = "https://github.com/smoltcp-rs/smoltcp.git", features = ["std"] }
|
||||
thiserror = "1.0"
|
||||
url = "2.3"
|
||||
|
||||
[dev-dependencies]
|
||||
|
|
80
src/error.rs
80
src/error.rs
|
@ -1,76 +1,50 @@
|
|||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
message: String,
|
||||
}
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("std::io::Error {0}")]
|
||||
IoError(#[from] std::io::Error),
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
From::<String>::from(err.to_string())
|
||||
}
|
||||
}
|
||||
#[error("std::net::AddrParseError {0}")]
|
||||
AddrParseError(#[from] std::net::AddrParseError),
|
||||
|
||||
impl From<std::net::AddrParseError> for Error {
|
||||
fn from(err: std::net::AddrParseError) -> Self {
|
||||
From::<String>::from(err.to_string())
|
||||
}
|
||||
}
|
||||
#[error("smoltcp::iface::RouteTableFull {0:?}")]
|
||||
RouteTableFull(#[from] smoltcp::iface::RouteTableFull),
|
||||
|
||||
impl From<smoltcp::iface::RouteTableFull> for Error {
|
||||
fn from(err: smoltcp::iface::RouteTableFull) -> Self {
|
||||
From::<String>::from(format!("{err:?}"))
|
||||
}
|
||||
}
|
||||
#[error("smoltcp::socket::tcp::RecvError {0:?}")]
|
||||
RecvError(#[from] smoltcp::socket::tcp::RecvError),
|
||||
|
||||
impl From<smoltcp::socket::tcp::RecvError> for Error {
|
||||
fn from(err: smoltcp::socket::tcp::RecvError) -> Self {
|
||||
From::<String>::from(format!("{err:?}"))
|
||||
}
|
||||
}
|
||||
#[error("smoltcp::socket::tcp::ListenError {0:?}")]
|
||||
ListenError(#[from] smoltcp::socket::tcp::ListenError),
|
||||
|
||||
impl From<smoltcp::socket::tcp::ListenError> for Error {
|
||||
fn from(err: smoltcp::socket::tcp::ListenError) -> Self {
|
||||
From::<String>::from(format!("{err:?}"))
|
||||
}
|
||||
}
|
||||
#[error("smoltcp::socket::udp::BindError {0:?}")]
|
||||
BindError(#[from] smoltcp::socket::udp::BindError),
|
||||
|
||||
impl From<smoltcp::socket::udp::BindError> for Error {
|
||||
fn from(err: smoltcp::socket::udp::BindError) -> Self {
|
||||
From::<String>::from(format!("{err:?}"))
|
||||
}
|
||||
}
|
||||
#[error("smoltcp::socket::tcp::SendError {0:?}")]
|
||||
SendError(#[from] smoltcp::socket::tcp::SendError),
|
||||
|
||||
impl From<smoltcp::socket::tcp::SendError> for Error {
|
||||
fn from(err: smoltcp::socket::tcp::SendError) -> Self {
|
||||
From::<String>::from(format!("{err:?}"))
|
||||
}
|
||||
#[error("&str {0}")]
|
||||
Str(String),
|
||||
|
||||
#[error("String {0}")]
|
||||
String(String),
|
||||
|
||||
#[error("&String {0}")]
|
||||
RefString(String),
|
||||
}
|
||||
|
||||
impl From<&str> for Error {
|
||||
fn from(err: &str) -> Self {
|
||||
From::<String>::from(err.to_string())
|
||||
Self::Str(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Error {
|
||||
fn from(err: String) -> Self {
|
||||
Self { message: err }
|
||||
Self::String(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&String> for Error {
|
||||
fn from(err: &String) -> Self {
|
||||
From::<String>::from(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
fn description(&self) -> &str {
|
||||
&self.message
|
||||
Self::RefString(err.to_string())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue