Skip to content

Commit 1cec37a

Browse files
Switch to thiserror for error handling
1 parent 0cb5e66 commit 1cec37a

File tree

5 files changed

+50
-74
lines changed

5 files changed

+50
-74
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog for Ferium
22

3+
## [3.7.2] - 06.11.2021
4+
5+
- Switched to `thiserror` for error handling
6+
37
## [3.7.1] - 06.11.2021
48

59
- Ferium now compiles successfully on Linux

Cargo.lock

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ferium"
3-
version = "3.7.1"
3+
version = "3.7.2"
44
edition = "2021"
55
authors = ["theRookieCoder <[email protected]>"]
66
description = "Ferium is an easy to use manager for Minecraft mods on Modrinth and Github Releases"
@@ -12,15 +12,15 @@ publish = false
1212
[dependencies]
1313
# Use `rustls` rather than OpenSSL
1414
reqwest = { version = "0", default-features = false, features = ["json", "rustls-tls"] }
15-
# Use bleeding edge version of `clap`
16-
clap = { git = "https://github.com/clap-rs/clap/", features = ["yaml"] }
1715
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
16+
clap = { version = "3.0.0-beta", features = ["yaml"] }
1817
serde = { version = "1", features = ["derive"] }
1918
fancy-regex = "0"
2019
shellexpand = "2"
2120
serde_json = "1"
2221
ansi_term = "0"
2322
dialoguer = "0"
23+
thiserror = "1"
2424
ferinth = "1"
2525
online = "3"
2626
bytes = "1"

src/main.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ use util::{
1818
};
1919

2020
#[tokio::main]
21-
async fn main() -> FResult<()> {
21+
async fn main() {
22+
if let Some(err) = actual_main().await.err() {
23+
println!("{}", err);
24+
std::process::exit(1);
25+
}
26+
}
27+
28+
async fn actual_main() -> FResult<()> {
2229
// Get the command to execute from Clap
2330
// This also displays help, version
2431
let command = cli::get_subcommand()?;

src/util/ferium_error.rs

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,48 @@
1-
use std::{
2-
convert::From,
3-
fmt::{Debug, Formatter},
4-
};
1+
use std::fmt::Debug;
2+
use thiserror::Error;
53

64
pub type FResult<T> = std::result::Result<T, FError>;
75

6+
#[derive(Error, Debug)]
87
pub enum FError {
98
/// 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")]
1010
EmptyConfigFile,
1111
/// 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),
1314
/// Failure to unwrap an Option, akin to `NullPointerError`s
15+
#[error("Could not access an expected value")]
1416
OptionError,
1517
/// Failed to parse a regular expression
16-
RegexError,
18+
#[error("Failed to parse regular expression")]
19+
RegexError(#[from] fancy_regex::Error),
1720
/// 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),
2136
/// An HTTP(S) request encountered an error
37+
#[error("An HTTP(S) request returned an error, {}", message)]
2238
HTTPError { message: String },
2339
/// 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),
2542
/// The program is running on an unsupported device
43+
#[error("The device you are currently running on is unsupported by Ferium")]
2644
InvalidDeviceError,
2745
/// The application should print `message` and quit (gracefully)
46+
#[error("{}", message)]
2847
Quit { message: String },
2948
}
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

Comments
 (0)