aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock3
-rw-r--r--Cargo.toml1
-rw-r--r--src/error.rs126
3 files changed, 99 insertions, 31 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 2554932..7e4fa96 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
-version = 3
+version = 4
[[package]]
name = "addr2line"
@@ -393,7 +393,6 @@ dependencies = [
"serde_json",
"socket2",
"sysinfo",
- "thiserror",
"tokio",
"trust-dns-proto",
]
diff --git a/Cargo.toml b/Cargo.toml
index 53ca284..b3a9712 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,6 +13,5 @@ rsdsl_pd_config = { git = "https://github.com/rsdsl/pd_config.git", version = "0
serde_json = "1.0"
socket2 = "0.5.3"
sysinfo = { version = "0.29.10", default-features = false }
-thiserror = "1.0"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread", "sync", "time", "signal"] }
trust-dns-proto = "0.22.0"
diff --git a/src/error.rs b/src/error.rs
index ffb57fe..61a8b14 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,48 +1,118 @@
-use std::{io, net, time};
+use std::{fmt, io, net, time};
use tokio::sync::watch;
-use thiserror::Error;
-
-#[derive(Debug, Error)]
+#[derive(Debug)]
pub enum Error {
- #[error("lease has been obtained but doesn't exist")]
LeaseNotFound,
- #[error("server did not include a client id option")]
NoClientId,
- #[error("server did not include a domain name servers option")]
NoDns,
- #[error("server did not include an ia_pd option")]
NoIAPD,
- #[error("server did not include an ia_prefix option in the ia_pd option")]
NoIAPrefix,
- #[error("server did not include a server id option")]
NoServerId,
- #[error("unable to send full packet (expected {0}, got {1})")]
PartialSend(usize, usize),
- #[error("too few domain name servers (expected at least 2, got {0})")]
TooFewDns(usize),
- #[error("received packet with wrong client duid (expected {0}, got {1})")]
WrongClientId(String, String),
- #[error("received packet with wrong server duid (expected {0}, got {1})")]
WrongServerId(String, String),
- #[error("can't parse network address: {0}")]
- AddrParse(#[from] net::AddrParseError),
- #[error("io error: {0}")]
- Io(#[from] io::Error),
- #[error("system time monotonicity error: {0}")]
- SystemTime(#[from] time::SystemTimeError),
+ AddrParse(net::AddrParseError),
+ Io(io::Error),
+ SystemTime(time::SystemTimeError),
+
+ WatchRecv(watch::error::RecvError),
+
+ DhcprotoDecode(dhcproto::error::DecodeError),
+ DhcprotoEncode(dhcproto::error::EncodeError),
+ SerdeJson(serde_json::Error),
+}
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Self::LeaseNotFound => write!(f, "lease has been obtained but doesn't exist")?,
+ Self::NoClientId => write!(f, "server did not include a client id option")?,
+ Self::NoDns => write!(f, "server did not include a domain name servers option")?,
+ Self::NoIAPD => write!(f, "server did not include an ia_pd option")?,
+ Self::NoIAPrefix => write!(
+ f,
+ "server did not include an ia_prefix option in the ia_pd option"
+ )?,
+ Self::NoServerId => write!(f, "server did not include a server id option")?,
+ Self::PartialSend(want, got) => write!(
+ f,
+ "unable to send full packet (expected {}, got {})",
+ want, got
+ )?,
+ Self::TooFewDns(n) => write!(
+ f,
+ "too few domain name servers (expected at least 2, got {})",
+ n
+ )?,
+ Self::WrongClientId(want, got) => write!(
+ f,
+ "received packet with wrong client duid (expected {}, got {})",
+ want, got
+ )?,
+ Self::WrongServerId(want, got) => write!(
+ f,
+ "received packet with wrong server duid (expected {}, got {})",
+ want, got
+ )?,
+ Self::AddrParse(e) => write!(f, "can't parse network address: {}", e)?,
+ Self::Io(e) => write!(f, "io error: {}", e)?,
+ Self::SystemTime(e) => write!(f, "system time monotonicity error: {}", e)?,
+ Self::WatchRecv(e) => write!(f, "can't receive from tokio watch channel: {}", e)?,
+ Self::DhcprotoDecode(e) => write!(f, "dhcproto decode error: {}", e)?,
+ Self::DhcprotoEncode(e) => write!(f, "dhcproto encode error: {}", e)?,
+ Self::SerdeJson(e) => write!(f, "serde_json error: {}", e)?,
+ }
- #[error("can't receive from tokio watch channel: {0}")]
- WatchRecv(#[from] watch::error::RecvError),
+ Ok(())
+ }
+}
+
+impl From<net::AddrParseError> for Error {
+ fn from(e: net::AddrParseError) -> Error {
+ Error::AddrParse(e)
+ }
+}
+
+impl From<io::Error> for Error {
+ fn from(e: io::Error) -> Error {
+ Error::Io(e)
+ }
+}
- #[error("dhcproto decode error: {0}")]
- DhcprotoDecode(#[from] dhcproto::error::DecodeError),
- #[error("dhcproto encode error: {0}")]
- DhcprotoEncode(#[from] dhcproto::error::EncodeError),
- #[error("serde_json error: {0}")]
- SerdeJson(#[from] serde_json::Error),
+impl From<time::SystemTimeError> for Error {
+ fn from(e: time::SystemTimeError) -> Error {
+ Error::SystemTime(e)
+ }
}
+impl From<watch::error::RecvError> for Error {
+ fn from(e: watch::error::RecvError) -> Error {
+ Error::WatchRecv(e)
+ }
+}
+
+impl From<dhcproto::error::DecodeError> for Error {
+ fn from(e: dhcproto::error::DecodeError) -> Error {
+ Error::DhcprotoDecode(e)
+ }
+}
+
+impl From<dhcproto::error::EncodeError> for Error {
+ fn from(e: dhcproto::error::EncodeError) -> Error {
+ Error::DhcprotoEncode(e)
+ }
+}
+
+impl From<serde_json::Error> for Error {
+ fn from(e: serde_json::Error) -> Error {
+ Error::SerdeJson(e)
+ }
+}
+
+impl std::error::Error for Error {}
+
pub type Result<T> = std::result::Result<T, Error>;