|
1 |
| -use std::{ |
2 |
| - convert::From, |
3 |
| - fmt::{Debug, Formatter}, |
4 |
| -}; |
| 1 | +use std::fmt::Debug; |
| 2 | +use thiserror::Error; |
5 | 3 |
|
6 | 4 | pub type FResult<T> = std::result::Result<T, FError>;
|
7 | 5 |
|
| 6 | +#[derive(Error, Debug)] |
8 | 7 | pub enum FError {
|
9 | 8 | /// The config file does not contain mods or repos
|
| 9 | + #[error("Your config file is empty! Run `ferium help` to see how to add mods or repositories")] |
10 | 10 | EmptyConfigFile,
|
11 | 11 | /// An HTTP(S) request returned with an error
|
12 |
| - ReqwestError { error: reqwest::Error }, |
| 12 | + #[error("Failed to send/process an HTTP(S) request due to {}", .0)] |
| 13 | + ReqwestError(#[from] reqwest::Error), |
13 | 14 | /// Failure to unwrap an Option, akin to `NullPointerError`s
|
| 15 | + #[error("Could not access an expected value")] |
14 | 16 | OptionError,
|
15 | 17 | /// Failed to parse a regular expression
|
16 |
| - RegexError, |
| 18 | + #[error("Failed to parse regular expression")] |
| 19 | + RegexError(#[from] fancy_regex::Error), |
17 | 20 | /// A JSON error occured
|
18 |
| - JsonError { |
19 |
| - category: serde_json::error::Category, |
20 |
| - }, |
| 21 | + #[error("{}", match .0.classify() { |
| 22 | + serde_json::error::Category::Syntax => { |
| 23 | + "Syntax error encountered in JSON file" |
| 24 | + }, |
| 25 | + serde_json::error::Category::Data => { |
| 26 | + "Non matching type while deserialising JSON" |
| 27 | + }, |
| 28 | + serde_json::error::Category::Eof => { |
| 29 | + "Unexpected end of file while reading JSON" |
| 30 | + }, |
| 31 | + serde_json::error::Category::Io => { |
| 32 | + "Encountered an Input/Output error while handling JSON" |
| 33 | + }, |
| 34 | + })] |
| 35 | + JsonError(#[from] serde_json::Error), |
21 | 36 | /// An HTTP(S) request encountered an error
|
| 37 | + #[error("An HTTP(S) request returned an error, {}", message)] |
22 | 38 | HTTPError { message: String },
|
23 | 39 | /// An I/O error occured
|
24 |
| - IOError { description: String }, |
| 40 | + #[error("Encountered an input/output error, {}", .0.to_string())] |
| 41 | + IOError(#[from] std::io::Error), |
25 | 42 | /// The program is running on an unsupported device
|
| 43 | + #[error("The device you are currently running on is unsupported by Ferium")] |
26 | 44 | InvalidDeviceError,
|
27 | 45 | /// The application should print `message` and quit (gracefully)
|
| 46 | + #[error("{}", message)] |
28 | 47 | Quit { message: String },
|
29 | 48 | }
|
30 |
| - |
31 |
| -impl Debug for FError { |
32 |
| - fn fmt(&self, fmt: &mut Formatter) -> Result<(), std::fmt::Error> { |
33 |
| - match self { |
34 |
| - FError::EmptyConfigFile => write!(fmt, "Your config file is empty! Run `ferium help` to see how to add mods or repositories"), |
35 |
| - FError::HTTPError { message } => write!(fmt, "An HTTP(S) request returned an error, {}", message), |
36 |
| - FError::InvalidDeviceError => write!(fmt, "The device you are currently running on is unsupported by Ferium"), |
37 |
| - FError::IOError {description} => write!(fmt, "Encountered an input/output error, {}", description), |
38 |
| - FError::JsonError { category } => match category { |
39 |
| - serde_json::error::Category::Syntax => { |
40 |
| - write!(fmt, "Syntax error encountered in JSON file") |
41 |
| - }, |
42 |
| - serde_json::error::Category::Data => { |
43 |
| - write!(fmt, "Non matching type while deserialising JSON") |
44 |
| - }, |
45 |
| - serde_json::error::Category::Eof => { |
46 |
| - write!(fmt, "Unexpected end of file while reading JSON") |
47 |
| - }, |
48 |
| - serde_json::error::Category::Io => { |
49 |
| - write!(fmt, "Encountered an Input/Output error while handling JSON") |
50 |
| - }, |
51 |
| - }, |
52 |
| - FError::OptionError => write!(fmt, "Could not access an expected value"), |
53 |
| - FError::Quit { message } => write!(fmt, "{}", message), |
54 |
| - FError::RegexError => write!(fmt, "Failed to parse regular expression"), |
55 |
| - FError::ReqwestError { error }=> write!(fmt, "Failed to send/process an HTTP(S) request due to {}", error), |
56 |
| - } |
57 |
| - } |
58 |
| -} |
59 |
| - |
60 |
| -impl From<reqwest::Error> for FError { |
61 |
| - fn from(err: reqwest::Error) -> Self { |
62 |
| - Self::ReqwestError { error: err } |
63 |
| - } |
64 |
| -} |
65 |
| - |
66 |
| -impl From<fancy_regex::Error> for FError { |
67 |
| - fn from(_: fancy_regex::Error) -> Self { |
68 |
| - Self::RegexError |
69 |
| - } |
70 |
| -} |
71 |
| - |
72 |
| -impl From<std::io::Error> for FError { |
73 |
| - fn from(err: std::io::Error) -> Self { |
74 |
| - Self::IOError { |
75 |
| - description: err.to_string(), |
76 |
| - } |
77 |
| - } |
78 |
| -} |
79 |
| - |
80 |
| -impl From<serde_json::Error> for FError { |
81 |
| - fn from(err: serde_json::Error) -> Self { |
82 |
| - Self::JsonError { |
83 |
| - category: err.classify(), |
84 |
| - } |
85 |
| - } |
86 |
| -} |
0 commit comments