1use std::{fmt, io};
11
12use crate::netlink::netlink::Nlmsgerr;
13
14pub struct NftnlError
16{
17 erno: i32,
19
20 err_msg: String,
22}
23
24impl NftnlError
25{
26 pub
28 fn new_internal_error(erno: i32, error_descr: String) -> Self
29 {
30 return Self{erno, err_msg: error_descr};
31 }
32
33 pub
35 fn new_ntfl_code(err: &Nlmsgerr, msg: String) -> Self
36 {
37 let ercode =
38 if err.get_error() < 0
39 {
40 -err.get_error()
41 }
42 else
43 {
44 err.get_error()
45 };
46
47 return Self{erno: ercode, err_msg: msg};
48 }
49
50 pub
52 fn get_erno(&self) -> i32
53 {
54 return self.erno;
55 }
56
57 pub
59 fn get_msg(&self) -> &str
60 {
61 return &self.err_msg;
62 }
63
64 pub
66 fn is_error_exists(&self) -> bool
67 {
68 return self.erno == libc::EEXIST;
69 }
70
71 pub
73 fn is_error_does_not_exist(&self) -> bool
74 {
75 return self.erno == libc::ENOENT;
76 }
77}
78
79impl fmt::Debug for NftnlError
80{
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
82 {
83 f
84 .debug_struct("NftnlError")
85 .field("erno", &self.erno)
86 .field("err_msg", &self.err_msg)
87 .finish()
88 }
89}
90
91impl fmt::Display for NftnlError
92{
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
94 {
95 write!(f, "InternalError: errno: '{}({})', msg: '{}'",
96 io::Error::from_raw_os_error(self.erno), self.erno, self.err_msg)
97 }
98}
99
100pub type NtflRes<R> = Result<R, NftnlError>;
101
102
103#[macro_export]
104macro_rules! int_error_code
105{
106 ($src:expr,$($arg:tt)*) => (
107 return std::result::Result::Err($crate::error::NftnlError::new_internal_error($src, format!($($arg)*)))
108 )
109}
110
111#[macro_export]
112macro_rules! map_int_error_code
113{
114 ($src:expr,$($arg:tt)*) => (
115 $crate::error::NftnlError::new_internal_error($src, format!($($arg)*))
116 )
117}
118
119#[macro_export]
120macro_rules! ntfl_error_code
121{
122 ($src:expr,$($arg:tt)*) => (
123 return std::result::Result::Err($crate::error::NftnlError::new_ntfl_code($src, format!($($arg)*)))
124 )
125}
126
127#[macro_export]
128macro_rules! map_ntfl_error_code
129{
130 ($src:expr,$($arg:tt)*) => (
131 $crate::error::NftnlError::new_ntfl_code($src, format!($($arg)*))
132 )
133}