1 | #[cfg (feature = "csv_output" )] |
2 | use csv::Error as CsvError; |
3 | use serde_json::Error as SerdeError; |
4 | use std::error::Error as StdError; |
5 | use std::fmt; |
6 | use std::io; |
7 | use std::path::PathBuf; |
8 | |
9 | #[allow (clippy::enum_variant_names)] |
10 | #[derive(Debug)] |
11 | pub enum Error { |
12 | AccessError { |
13 | path: PathBuf, |
14 | inner: io::Error, |
15 | }, |
16 | CopyError { |
17 | from: PathBuf, |
18 | to: PathBuf, |
19 | inner: io::Error, |
20 | }, |
21 | SerdeError { |
22 | path: PathBuf, |
23 | inner: SerdeError, |
24 | }, |
25 | #[cfg (feature = "csv_output" )] |
26 | /// This API requires the following crate features to be activated: csv_output |
27 | CsvError(CsvError), |
28 | } |
29 | impl fmt::Display for Error { |
30 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
31 | match self { |
32 | Error::AccessError { path, inner } => { |
33 | write!(f, "Failed to access file {:?}: {}" , path, inner) |
34 | } |
35 | Error::CopyError { from, to, inner } => { |
36 | write!(f, "Failed to copy file {:?} to {:?}: {}" , from, to, inner) |
37 | } |
38 | Error::SerdeError { path, inner } => write!( |
39 | f, |
40 | "Failed to read or write file {:?} due to serialization error: {}" , |
41 | path, inner |
42 | ), |
43 | #[cfg (feature = "csv_output" )] |
44 | Error::CsvError(inner) => write!(f, "CSV error: {}" , inner), |
45 | } |
46 | } |
47 | } |
48 | impl StdError for Error { |
49 | fn description(&self) -> &str { |
50 | match self { |
51 | Error::AccessError { .. } => "AccessError" , |
52 | Error::CopyError { .. } => "CopyError" , |
53 | Error::SerdeError { .. } => "SerdeError" , |
54 | #[cfg (feature = "csv_output" )] |
55 | Error::CsvError(_) => "CsvError" , |
56 | } |
57 | } |
58 | |
59 | fn cause(&self) -> Option<&dyn StdError> { |
60 | match self { |
61 | Error::AccessError { inner, .. } => Some(inner), |
62 | Error::CopyError { inner, .. } => Some(inner), |
63 | Error::SerdeError { inner, .. } => Some(inner), |
64 | #[cfg (feature = "csv_output" )] |
65 | Error::CsvError(inner) => Some(inner), |
66 | } |
67 | } |
68 | } |
69 | |
70 | #[cfg (feature = "csv_output" )] |
71 | impl From<CsvError> for Error { |
72 | fn from(other: CsvError) -> Error { |
73 | Error::CsvError(other) |
74 | } |
75 | } |
76 | |
77 | pub type Result<T> = ::std::result::Result<T, Error>; |
78 | |
79 | pub(crate) fn log_error(e: &Error) { |
80 | error!("error: {}" , e); |
81 | } |
82 | |