cdns_rs/
error.rs

1/*-
2 * cdns-rs - a simple sync/async DNS query library
3 * 
4 * Copyright (C) 2020  Aleksandr Morozov
5 * 
6 * Copyright 2025 Aleksandr Morozov
7 * 
8 * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
9 * the European Commission - subsequent versions of the EUPL (the "Licence").
10 * 
11 * You may not use this work except in compliance with the Licence.
12 * 
13 * You may obtain a copy of the Licence at:
14 * 
15 *    https://joinup.ec.europa.eu/software/page/eupl
16 * 
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20 * Licence for the specific language governing permissions and limitations
21 * under the Licence.
22 */
23
24use 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    /// Error related to DNS response parsing
98    DnsResponse,
99    /// Received response with unknown ID in header
100    RespIdMismatch,
101    /// Internal error (assertions...)
102    InternalError,
103    /// Socket, File error
104    IoError,
105    /// Response was truncated
106    MessageTruncated,
107    /// Timeout event
108    RequestTimeout,
109    /// Error in configuraion file (format)
110    ConfigError,
111    /// Nameservers unreachable
112    DnsNotAvailable,
113    /// HTTP Errors
114    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}