fern/
errors.rs

1use std::{error, fmt, io};
2
3/// Convenience error combining possible errors which could occur while
4/// initializing logging.
5///
6/// Fern does not use this error natively, but functions which set up fern and
7/// open log files will often need to return both [`io::Error`] and
8/// [`SetLoggerError`]. This error is for that purpose.
9///
10/// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html
11/// [`SetLoggerError`]: ../log/struct.SetLoggerError.html
12#[derive(Debug)]
13pub enum InitError {
14    /// IO error.
15    Io(io::Error),
16    /// The log crate's global logger was already initialized when trying to
17    /// initialize a logger.
18    SetLoggerError(log::SetLoggerError),
19}
20
21impl From<io::Error> for InitError {
22    fn from(error: io::Error) -> InitError {
23        InitError::Io(error)
24    }
25}
26
27impl From<log::SetLoggerError> for InitError {
28    fn from(error: log::SetLoggerError) -> InitError {
29        InitError::SetLoggerError(error)
30    }
31}
32
33impl fmt::Display for InitError {
34    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
35        match *self {
36            InitError::Io(ref e) => write!(f, "IO Error initializing logger: {}", e),
37            InitError::SetLoggerError(ref e) => write!(f, "logging initialization failed: {}", e),
38        }
39    }
40}
41
42impl error::Error for InitError {
43    fn description(&self) -> &str {
44        match *self {
45            InitError::Io(..) => "IO error while initializing logging",
46            InitError::SetLoggerError(..) => {
47                "logging system already initialized with different logger"
48            }
49        }
50    }
51
52    fn cause(&self) -> Option<&dyn error::Error> {
53        match *self {
54            InitError::Io(ref e) => Some(e),
55            InitError::SetLoggerError(ref e) => Some(e),
56        }
57    }
58}