1use std::fmt;
2use std::result;
3
4use failure::{Backtrace, Context, Fail};
5
6pub type Result<T> = result::Result<T, Error>;
8
9#[derive(Debug)]
11pub struct Error {
12 inner: Context<ErrorKind>,
13}
14
15impl Error {
16 pub fn kind(&self) -> ErrorKind {
17 *self.inner.get_context()
18 }
19}
20
21impl Fail for Error {
22 fn cause(&self) -> Option<&dyn Fail> {
23 self.inner.cause()
24 }
25 fn backtrace(&self) -> Option<&Backtrace> {
26 self.inner.backtrace()
27 }
28}
29
30impl fmt::Display for Error {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 write!(f, "{}", self.kind())
33 }
34}
35
36#[derive(Debug, Copy, Clone, PartialEq, Fail)]
37pub enum ErrorKind {
38 ConfigError,
39 PoisonError,
40 IoError,
41 ClapError,
42 LoggerError,
43 AnyhowError,
44}
45
46impl fmt::Display for ErrorKind {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 write!(f, "Undefined Error")
49 }
50}
51
52impl From<ErrorKind> for Error {
53 fn from(kind: ErrorKind) -> Error {
54 Error {
55 inner: Context::new(kind),
56 }
57 }
58}
59
60impl From<Context<ErrorKind>> for Error {
61 fn from(inner: failure::Context<ErrorKind>) -> Error {
62 Error { inner }
63 }
64}
65
66impl From<config::ConfigError> for Error {
67 fn from(err: config::ConfigError) -> Self {
68 Error {
69 inner: err.context(ErrorKind::ConfigError),
70 }
71 }
72}
73
74impl<T> From<std::sync::PoisonError<T>> for Error {
75 fn from(_err: std::sync::PoisonError<T>) -> Self {
76 Error {
77 inner: Context::from(ErrorKind::PoisonError),
78 }
79 }
80}
81
82impl From<std::io::Error> for Error {
83 fn from(err: std::io::Error) -> Self {
84 Error {
85 inner: err.context(ErrorKind::IoError),
86 }
87 }
88}
89
90impl From<clap::Error> for Error {
91 fn from(err: clap::Error) -> Self {
92 Error {
93 inner: err.context(ErrorKind::ClapError),
94 }
95 }
96}
97
98impl From<log::SetLoggerError> for Error {
99 fn from(err: log::SetLoggerError) -> Self {
100 Error {
101 inner: err.context(ErrorKind::LoggerError),
102 }
103 }
104}
105
106impl From<anyhow::Error> for Error {
107 fn from(err: anyhow::Error) -> Self {
108 eprintln!("{err:?}");
109 Error {
110 inner: Context::from(ErrorKind::AnyhowError),
111 }
112 }
113}