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