libhoney/
errors.rs

1use std::fmt;
2use std::io;
3
4/// Result shorthand for a `std::result::Result` wrapping our own `Error`
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// Type of error, exposed through `Error` member `kind`
8#[derive(Debug, Copy, Clone, PartialEq)]
9pub enum ErrorKind {
10    /// Event has no populated fields
11    MissingEventFields,
12
13    /// Mandatory client/transmission Option is missing
14    MissingOption,
15
16    /// User Func error
17    UserFuncError,
18
19    /// Sender full
20    ChannelError,
21
22    /// Any IO related error
23    Io,
24}
25
26/// Error
27#[derive(Debug)]
28pub struct Error {
29    /// Error message
30    pub message: String,
31    /// Type of error
32    pub kind: ErrorKind,
33}
34
35impl Error {
36    #[doc(hidden)]
37    pub(crate) fn missing_event_fields() -> Self {
38        Self {
39            message: String::from("event has no data"),
40            kind: ErrorKind::MissingEventFields,
41        }
42    }
43
44    #[doc(hidden)]
45    pub(crate) fn missing_option(option: &str, extra: &str) -> Self {
46        Self {
47            message: format!("missing option '{}', {}", option, extra),
48            kind: ErrorKind::MissingOption,
49        }
50    }
51
52    #[doc(hidden)]
53    pub(crate) fn sender_full(sender: &str) -> Self {
54        Self {
55            message: format!("sender '{}' is full", sender),
56            kind: ErrorKind::ChannelError,
57        }
58    }
59
60    #[doc(hidden)]
61    pub(crate) fn with_description(description: &str, kind: ErrorKind) -> Self {
62        Self {
63            message: format!("error: {}", description),
64            kind,
65        }
66    }
67}
68
69impl std::error::Error for Error {
70    fn description(&self) -> &str {
71        &*self.message
72    }
73}
74
75impl fmt::Display for Error {
76    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77        writeln!(f, "{}", self.message)
78    }
79}
80
81impl From<io::Error> for Error {
82    fn from(e: io::Error) -> Self {
83        Self::with_description(&e.to_string(), ErrorKind::Io)
84    }
85}
86
87impl<T> From<crossbeam_channel::SendError<T>> for Error {
88    fn from(e: crossbeam_channel::SendError<T>) -> Self {
89        Self::with_description(&e.to_string(), ErrorKind::ChannelError)
90    }
91}