use std::{error::Error, fmt::{self}};
pub(crate)
fn map_read_err(e: std::io::Error) -> CDnsError
{
return CDnsError::new(CDnsErrorType::IoError, format!("{}", e));
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CDnsSuperError(CDnsError);
impl Error for CDnsSuperError
{
fn source(&self) -> Option<&(dyn Error + 'static)>
{
Some(&self.0)
}
}
impl fmt::Display for CDnsSuperError
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "{}", self.source().unwrap())
}
}
impl From<CDnsError> for CDnsSuperError
{
fn from(value: CDnsError) -> Self
{
return Self(value);
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct CDnsError
{
pub err_code: CDnsErrorType,
pub message: String,
}
impl CDnsError
{
pub fn new(err_code: CDnsErrorType, msg: String) -> Self
{
return CDnsError{err_code: err_code, message: msg};
}
}
impl Error for CDnsError {}
impl fmt::Display for CDnsError
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "cdns: [{}], {}", self.err_code, self.message)
}
}
impl fmt::Debug for CDnsError
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
write!(f, "cdns: [{}], {}", self.err_code, self.message)
}
}
#[derive(Clone, PartialEq, Eq)]
pub enum CDnsErrorType
{
DnsResponse,
RespIdMismatch,
InternalError,
IoError,
MessageTruncated,
RequestTimeout,
ConfigError,
DnsNotAvailable,
HttpError,
SocketNotSupported,
PunycodeNotAscii,
}
impl fmt::Display for CDnsErrorType
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
match *self
{
Self::DnsResponse =>
write!(f, "DNS response"),
Self::RespIdMismatch =>
write!(f, "Response ID mismatch"),
Self::InternalError =>
write!(f, "Internal Error"),
Self::IoError =>
write!(f, "IO Error"),
Self::MessageTruncated =>
write!(f, "Message was truncated"),
Self::RequestTimeout =>
write!(f, "Request receive timout"),
Self::ConfigError =>
write!(f, "Config file error"),
Self::DnsNotAvailable =>
write!(f, "DNS not available"),
Self::HttpError =>
write!(f, "HTTP Error"),
Self::SocketNotSupported =>
write!(f, "Socket connection is not supported"),
Self::PunycodeNotAscii =>
write!(f, "Received punycode with non ASCII characters"),
}
}
}
pub type CDnsResult<T> = Result<T, CDnsError>;
#[macro_export]
macro_rules! internal_error
{
($src:expr,$($arg:tt)*) => (
return std::result::Result::Err($crate::CDnsError::new($src, format!($($arg)*)))
)
}
#[macro_export]
macro_rules! internal_error_map
{
($src:expr,$($arg:tt)*) => (
$crate::CDnsError::new($src, format!($($arg)*))
)
}
#[cfg(feature = "no_error_output")]
#[macro_export]
macro_rules! write_error
{
($src:expr) => (
{let _ = $src;}
)
}
#[cfg(not(feature = "no_error_output"))]
#[macro_export]
macro_rules! write_error
{
($src:expr) => (
eprintln!("{}", $src)
)
}