syslog_rs/
error.rs

1/*-
2 * syslog-rs - a syslog client translated from libc to rust
3 * 
4 * Copyright 2025 Aleksandr Morozov
5 * 
6 * The syslog-rs crate can be redistributed and/or modified
7 * under the terms of either of the following licenses:
8 *
9 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
10 *                     
11 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
12 */
13
14
15use std::{fmt, io::ErrorKind};
16
17use nix::errno::Errno;
18
19/// Error code
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub enum SyslogErrCode
22{
23    /// Os Error
24    OsError(ErrorKind),
25
26    /// Os Error
27    OsErrorErrno(Errno),
28
29    /// A message which can be output to stderr
30    InternalError,
31
32    /// Only for syslog_sync_queue.rs and returned only when syslog thread is stopped
33    /// and someone is trying to do some operation
34    SyslogThreadNotAvailable,
35
36    /// Only for syslog_sync_queue.rs and returned only when syslog thread is failed
37    /// to send back data
38    UnboundedChannelError,
39
40    /// Mutex poisoned, can not contunue
41    MutexPoisoned,
42
43    /// The function is not usable.
44    Unusable,
45
46    /// Channel errors during transmission.
47    SendError,
48
49    /// Functionality os not available.
50    NotAvail,
51
52    CowTransactionFailed,
53
54    NoCowLockFailed,
55}
56
57impl fmt::Display for SyslogErrCode
58{
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
60    {
61        match self
62        {
63            Self::OsError(error_kind) => 
64                write!(f, "[ErrorKind: {}]", error_kind),
65            Self::InternalError => 
66                write!(f, "[InternalError]"),
67            Self::SyslogThreadNotAvailable => 
68                write!(f, "[SyslogThreadNotAvailable]"),
69            Self::UnboundedChannelError => 
70                write!(f, "[UnboundedChannelError]"),
71            Self::MutexPoisoned => 
72                write!(f, "[MutexPoisoned]"),
73            Self::OsErrorErrno(errn) =>
74                write!(f, "[Errno: {}]", errn),
75            Self::Unusable => 
76                write!(f, "[Unusable]"),
77            Self::SendError =>
78                write!(f, "[SendError]"),
79            Self::NotAvail => 
80                write!(f, "[NotAvail]"),
81            Self::CowTransactionFailed => 
82                write!(f, "[CowTransactionFailed]"),
83            Self::NoCowLockFailed => 
84                write!(f, "[NoCowLockFailed]"),
85        }
86    }
87}
88
89impl SyslogErrCode
90{
91    pub 
92    fn get_os_err_code(&self) -> Option<ErrorKind>
93    {
94        match *self
95        {
96            Self::OsError(errn) => 
97                return Some(errn),
98            _ =>
99                return None,
100        }
101    }
102}
103
104/// A syslog crate error
105#[derive(Debug)]
106pub struct SyslogError 
107{
108    errcode: SyslogErrCode,
109    message: String,
110}
111
112impl SyslogError
113{
114    /// Creates new error
115    pub
116    fn new(errcode: SyslogErrCode, msg: String) -> Self
117    {
118        return SyslogError{errcode: errcode, message: msg};
119    }
120
121    /// Creates new error
122    pub
123    fn new_io(err: &std::io::Error, msg: String) -> Self
124    {
125        return SyslogError{errcode: SyslogErrCode::OsError(err.kind()), message: msg};
126    }
127
128    pub
129    fn new_errno(err: Errno, msg: String) -> Self
130    {
131        return SyslogError{errcode: SyslogErrCode::OsErrorErrno(err), message: msg};
132    }
133
134    /// Retuns a clone of [SyslogErrCode]
135    pub 
136    fn get_errcode(&self) -> SyslogErrCode
137    {
138        return self.errcode;
139    }
140
141    /// Moves out the error description
142    pub 
143    fn into_inner(self) -> String
144    {
145        return self.message;
146    }
147}
148
149impl fmt::Display for SyslogError 
150{
151    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result 
152    {
153        write!(f, "{} {}", self.errcode, self.message)
154    }
155}
156
157pub type SyRes<T> = Result<T, SyslogError>;
158
159#[macro_export]
160macro_rules! throw_error 
161{
162    ($($arg:tt)*) => (
163        return std::result::Result::Err($crate::error::SyslogError::new($crate::error::SyslogErrCode::InternalError, format!($($arg)*)))
164    )
165}
166
167#[macro_export]
168macro_rules! throw_error_os
169{
170    ($err:expr, $($arg:tt)*) => (
171        return std::result::Result::Err($crate::error::SyslogError::new_io(&$err, format!($($arg)*)))
172    )
173}
174
175#[macro_export]
176macro_rules! throw_error_errno
177{
178    ($errno:expr, $($arg:tt)*) => (
179        return std::result::Result::Err($crate::error::SyslogError::new_errno($errno, format!($($arg)*)))
180    )
181}
182
183#[macro_export]
184macro_rules! map_error 
185{
186    ($($arg:tt)*) => (
187        $crate::error::SyslogError::new($crate::error::SyslogErrCode::InternalError, format!($($arg)*))
188    )
189}
190
191#[macro_export]
192macro_rules! map_error_os
193{
194    ($err:expr, $($arg:tt)*) => (
195        $crate::error::SyslogError::new_io(&$err, format!($($arg)*))
196    )
197}
198
199#[macro_export]
200macro_rules! throw_error_code
201{
202    ($code:tt, $($arg:tt)*) => (
203        return std::result::Result::Err($crate::error::SyslogError::new($crate::error::SyslogErrCode::$code, format!($($arg)*)))
204    )
205}
206
207#[macro_export]
208macro_rules! map_error_code
209{
210    ($code:tt, $($arg:tt)*) => (
211        $crate::error::SyslogError::new($crate::error::SyslogErrCode::$code, format!($($arg)*))
212    )
213}