nftnl_rs/
error.rs

1/*-
2 * nftnl-rs - a netlink NFtables firewall.
3 * Copyright (C) 2020 Aleksandr Morozov
4 * 
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 *  file, You can obtain one at https://mozilla.org/MPL/2.0/.
8 */
9
10use std::{fmt, io};
11
12use crate::netlink::netlink::Nlmsgerr;
13
14/// A storage for error and description.
15pub struct NftnlError
16{
17    /// errno
18    erno: i32,
19
20    /// message
21    err_msg: String,
22}
23
24impl NftnlError
25{
26    /// Creates new error from errno and user provided message.
27    pub 
28    fn new_internal_error(erno: i32, error_descr: String) -> Self
29    {
30        return Self{erno, err_msg: error_descr};
31    }
32
33    /// Creates error from [Nlmsgerr] and user provided message.
34    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    /// Returns raw errno.
51    pub 
52    fn get_erno(&self) -> i32
53    {
54        return self.erno;
55    }
56
57    /// Returns the error message (error description privided in the program.)
58    pub 
59    fn get_msg(&self) -> &str
60    {
61        return &self.err_msg;
62    }
63
64    /// Test is error is [libc::EEXIST]
65    pub 
66    fn is_error_exists(&self) -> bool
67    {
68        return self.erno == libc::EEXIST;
69    }
70
71    /// Test is error is [libc::ENOENT]
72    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}