libhoney/
errors.rs

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
use std::fmt;
use std::io;

/// Result shorthand for a `std::result::Result` wrapping our own `Error`
pub type Result<T> = std::result::Result<T, Error>;

/// Type of error, exposed through `Error` member `kind`
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ErrorKind {
    /// Event has no populated fields
    MissingEventFields,

    /// Mandatory client/transmission Option is missing
    MissingOption,

    /// User Func error
    UserFuncError,

    /// Sender full
    ChannelError,

    /// Any IO related error
    Io,
}

/// Error
#[derive(Debug)]
pub struct Error {
    /// Error message
    pub message: String,
    /// Type of error
    pub kind: ErrorKind,
}

impl Error {
    #[doc(hidden)]
    pub(crate) fn missing_event_fields() -> Self {
        Self {
            message: String::from("event has no data"),
            kind: ErrorKind::MissingEventFields,
        }
    }

    #[doc(hidden)]
    pub(crate) fn missing_option(option: &str, extra: &str) -> Self {
        Self {
            message: format!("missing option '{}', {}", option, extra),
            kind: ErrorKind::MissingOption,
        }
    }

    #[doc(hidden)]
    pub(crate) fn sender_full(sender: &str) -> Self {
        Self {
            message: format!("sender '{}' is full", sender),
            kind: ErrorKind::ChannelError,
        }
    }

    #[doc(hidden)]
    pub(crate) fn with_description(description: &str, kind: ErrorKind) -> Self {
        Self {
            message: format!("error: {}", description),
            kind,
        }
    }
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        &*self.message
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{}", self.message)
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Self::with_description(&e.to_string(), ErrorKind::Io)
    }
}

impl<T> From<crossbeam_channel::SendError<T>> for Error {
    fn from(e: crossbeam_channel::SendError<T>) -> Self {
        Self::with_description(&e.to_string(), ErrorKind::ChannelError)
    }
}