1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use std::error;
use std::fmt;
use std::panic::PanicInfo;
use std::str::Utf8Error;

/// Error that occurred while translating the collectd config to rust structures.
#[derive(Debug, Clone)]
pub enum ConfigError {
    /// The config type (eg: string, number, etc) denoted is unrecognized
    UnknownType(i32),

    /// The config string contains invalid UTF-8 characters
    StringDecode(Utf8Error),
}

impl fmt::Display for ConfigError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            ConfigError::UnknownType(type_) => {
                write!(f, "unknown value ({}) for config enum", type_)
            }
            ConfigError::StringDecode(ref _e) => {
                write!(f, "unable to convert config string to utf8")
            }
        }
    }
}

impl error::Error for ConfigError {
    fn description(&self) -> &str {
        "error interpreting configuration values"
    }

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            ConfigError::StringDecode(ref e) => Some(e),
            ConfigError::UnknownType(_) => None,
        }
    }
}

/// Error that occurred when converting a rust UTF-8 string to an array of `c_char` for collectd
/// ingestion.
#[derive(Debug, Clone)]
pub enum ArrayError {
    /// The UTF-8 string contained a null character
    NullPresent(usize, String),

    /// The UTF-8 string is too long to be ingested
    TooLong(usize),
}

impl fmt::Display for ArrayError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            ArrayError::NullPresent(pos, ref s) => {
                write!(f, "null encountered (pos: {}) in string: {}", pos, s)
            }
            ArrayError::TooLong(len) => write!(f, "length of {} is too long", len),
        }
    }
}

impl error::Error for ArrayError {
    fn description(&self) -> &str {
        "error generating array"
    }
}

/// Error that occurred while receiving values from collectd to write
#[derive(Debug, Clone)]
pub enum ReceiveError {
    /// A plugin submitted a field that contained invalid UTF-8 characters
    Utf8(String, &'static str, Utf8Error),
}

impl fmt::Display for ReceiveError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            ReceiveError::Utf8(ref plugin, ref field, ref _err) => {
                write!(f, "plugin: {} submitted bad field: {}", plugin, field)
            }
        }
    }
}

impl error::Error for ReceiveError {
    fn description(&self) -> &str {
        "error generating a value list"
    }

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            ReceiveError::Utf8(ref _plugin, ref _field, ref err) => Some(err),
        }
    }
}

/// Errors that occur when submitting values to collectd
#[derive(Debug, Clone)]
pub enum SubmitError {
    /// Contains the exit status that collectd returns when a submission fails
    Dispatch(i32),

    Field(&'static str, ArrayError),
}

impl fmt::Display for SubmitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            SubmitError::Dispatch(code) => {
                write!(f, "plugin_dispatch_values returned an error: {}", code)
            }
            SubmitError::Field(ref field, ref _err) => write!(f, "error submitting {}", field),
        }
    }
}

impl error::Error for SubmitError {
    fn description(&self) -> &str {
        "error generating array"
    }

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            SubmitError::Dispatch(_code) => None,
            SubmitError::Field(_field, ref err) => Some(err),
        }
    }
}

/// If a plugin advertises that it supports a certain functionality, but doesn't implement the
/// necessary `Plugin` function, this error is returned.
#[derive(Clone, Copy, Debug)]
pub struct NotImplemented;

impl fmt::Display for NotImplemented {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "function is not implemented")
    }
}

impl error::Error for NotImplemented {
    fn description(&self) -> &str {
        "function is not implemented"
    }
}

/// Errors that occur when retrieving rates
#[derive(Clone, Debug)]
pub struct CacheRateError;

impl fmt::Display for CacheRateError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "unable to retrieve rate (see collectd logs for additional details)"
        )
    }
}

impl error::Error for CacheRateError {
    fn description(&self) -> &str {
        "unable to retrieve rate (see collectd logs for additional details)"
    }
}

/// Errors that occur on the boundary between collectd and a plugin
#[derive(Debug)]
pub enum FfiError<'a> {
    /// Error for implementing Rust's panic hook
    PanicHook(&'a PanicInfo<'a>),

    /// Represents a plugin that panicked. A plugin that panics has a logic bug that should be
    /// fixed so that the plugin can better log and recover, else collectd decides
    Panic,

    /// An error from the plugin. This is a "normal" error that the plugin has caught. Like if the
    /// database is down and the plugin has the proper error mechanisms
    Plugin(Box<dyn error::Error>),

    /// An error occurred outside the path of a plugin
    Collectd(Box<dyn error::Error>),

    /// When logging, collectd handed us a log level that was outside the known range
    UnknownSeverity(i32),

    /// Collectd gave us multiple configs to deserialize
    MultipleConfig,

    /// Collectd gave us field that contains invalid UTF-8 characters
    Utf8(&'static str, Utf8Error),
}

impl<'a> fmt::Display for FfiError<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            FfiError::Collectd(_) => write!(f, "unexpected collectd behavior"),
            FfiError::UnknownSeverity(severity) => {
                write!(f, "unrecognized severity level: {}", severity)
            }
            FfiError::MultipleConfig => write!(f, "duplicate config section"),
            FfiError::Panic => write!(f, "plugin panicked"),
            FfiError::PanicHook(info) => {
                write!(f, "plugin panicked: ")?;
                if let Some(location) = info.location() {
                    write!(f, "({}: {}): ", location.file(), location.line(),)?;
                }

                if let Some(payload) = info.payload().downcast_ref::<&str>() {
                    write!(f, "{}", payload)?;
                }

                Ok(())
            }
            FfiError::Plugin(_) => write!(f, "plugin encountered an error"),
            FfiError::Utf8(field, ref _e) => write!(f, "UTF-8 error for field: {}", field),
        }
    }
}

impl<'a> error::Error for FfiError<'a> {
    fn description(&self) -> &str {
        "collectd plugin error"
    }

    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            FfiError::Collectd(ref e) => Some(e.as_ref()),
            FfiError::Plugin(ref e) => Some(e.as_ref()),
            FfiError::Utf8(_field, ref e) => Some(e),
            _ => None,
        }
    }
}