1use std::{error::Error, fmt::{self}};
25
26
27pub(crate)
28fn map_read_err(e: std::io::Error) -> CDnsError
29{
30 return CDnsError::new(CDnsErrorType::IoError, format!("{}", e));
31}
32
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct CDnsSuperError(CDnsError);
35
36impl Error for CDnsSuperError
37{
38 fn source(&self) -> Option<&(dyn Error + 'static)>
39 {
40 Some(&self.0)
41 }
42}
43
44impl fmt::Display for CDnsSuperError
45{
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
47 {
48 write!(f, "{}", self.source().unwrap())
49 }
50}
51
52impl From<CDnsError> for CDnsSuperError
53{
54 fn from(value: CDnsError) -> Self
55 {
56 return Self(value);
57 }
58}
59
60#[derive(Clone, PartialEq, Eq)]
61pub struct CDnsError
62{
63 pub err_code: CDnsErrorType,
64 pub message: String,
65}
66
67impl CDnsError
68{
69 pub fn new(err_code: CDnsErrorType, msg: String) -> Self
70 {
71 return CDnsError{err_code: err_code, message: msg};
72 }
73}
74
75impl Error for CDnsError {}
76
77impl fmt::Display for CDnsError
78{
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
80 {
81 write!(f, "cdns: [{}], {}", self.err_code, self.message)
82 }
83}
84impl fmt::Debug for CDnsError
85{
86 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
87 {
88 write!(f, "cdns: [{}], {}", self.err_code, self.message)
89 }
90}
91
92
93
94#[derive(Clone, PartialEq, Eq)]
95pub enum CDnsErrorType
96{
97 DnsResponse,
99 RespIdMismatch,
101 InternalError,
103 IoError,
105 MessageTruncated,
107 RequestTimeout,
109 ConfigError,
111 DnsNotAvailable,
113 HttpError,
115}
116
117impl fmt::Display for CDnsErrorType
118{
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
120 {
121 match *self
122 {
123 Self::DnsResponse => write!(f, "DNS response"),
124 Self::RespIdMismatch => write!(f, "Response ID mismatch"),
125 Self::InternalError => write!(f, "Internal Error"),
126 Self::IoError => write!(f, "IO Error"),
127 Self::MessageTruncated => write!(f, "Message was truncated"),
128 Self::RequestTimeout => write!(f, "Request receive timout"),
129 Self::ConfigError => write!(f, "Config file error"),
130 Self::DnsNotAvailable => write!(f, "DNS not available"),
131 Self::HttpError => write!(f, "HTTP Error"),
132 }
133 }
134}
135
136pub type CDnsResult<T> = Result<T, CDnsError>;
137
138#[macro_export]
139macro_rules! internal_error
140{
141 ($src:expr,$($arg:tt)*) => (
142 return std::result::Result::Err($crate::CDnsError::new($src, format!($($arg)*)))
143 )
144}
145
146#[macro_export]
147macro_rules! internal_error_map
148{
149 ($src:expr,$($arg:tt)*) => (
150 $crate::CDnsError::new($src, format!($($arg)*))
151 )
152}
153
154#[cfg(feature = "no_error_output")]
155#[macro_export]
156macro_rules! write_error
157{
158 ($src:expr) => (
159 {let _ = $src;}
160 )
161}
162
163#[cfg(not(feature = "no_error_output"))]
164#[macro_export]
165macro_rules! write_error
166{
167 ($src:expr) => (
168 eprintln!("{}", $src)
169 )
170}