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 SocketNotSupported
117}
118
119impl fmt::Display for CDnsErrorType
120{
121 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
122 {
123 match *self
124 {
125 Self::DnsResponse => write!(f, "DNS response"),
126 Self::RespIdMismatch => write!(f, "Response ID mismatch"),
127 Self::InternalError => write!(f, "Internal Error"),
128 Self::IoError => write!(f, "IO Error"),
129 Self::MessageTruncated => write!(f, "Message was truncated"),
130 Self::RequestTimeout => write!(f, "Request receive timout"),
131 Self::ConfigError => write!(f, "Config file error"),
132 Self::DnsNotAvailable => write!(f, "DNS not available"),
133 Self::HttpError => write!(f, "HTTP Error"),
134 Self::SocketNotSupported => write!(f, "Socket connection is not supported")
135 }
136 }
137}
138
139pub type CDnsResult<T> = Result<T, CDnsError>;
140
141#[macro_export]
142macro_rules! internal_error
143{
144 ($src:expr,$($arg:tt)*) => (
145 return std::result::Result::Err($crate::CDnsError::new($src, format!($($arg)*)))
146 )
147}
148
149#[macro_export]
150macro_rules! internal_error_map
151{
152 ($src:expr,$($arg:tt)*) => (
153 $crate::CDnsError::new($src, format!($($arg)*))
154 )
155}
156
157#[cfg(feature = "no_error_output")]
158#[macro_export]
159macro_rules! write_error
160{
161 ($src:expr) => (
162 {let _ = $src;}
163 )
164}
165
166#[cfg(not(feature = "no_error_output"))]
167#[macro_export]
168macro_rules! write_error
169{
170 ($src:expr) => (
171 eprintln!("{}", $src)
172 )
173}