Skip to content

Commit

Permalink
Switch to thiserror for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
theRookieCoder committed Nov 6, 2021
1 parent 0cb5e66 commit 1cec37a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 74 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for Ferium

## [3.7.2] - 06.11.2021

- Switched to `thiserror` for error handling

## [3.7.1] - 06.11.2021

- Ferium now compiles successfully on Linux
Expand Down
9 changes: 6 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ferium"
version = "3.7.1"
version = "3.7.2"
edition = "2021"
authors = ["theRookieCoder <[email protected]>"]
description = "Ferium is an easy to use manager for Minecraft mods on Modrinth and Github Releases"
Expand All @@ -12,15 +12,15 @@ publish = false
[dependencies]
# Use `rustls` rather than OpenSSL
reqwest = { version = "0", default-features = false, features = ["json", "rustls-tls"] }
# Use bleeding edge version of `clap`
clap = { git = "https://github.com/clap-rs/clap/", features = ["yaml"] }
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
clap = { version = "3.0.0-beta", features = ["yaml"] }
serde = { version = "1", features = ["derive"] }
fancy-regex = "0"
shellexpand = "2"
serde_json = "1"
ansi_term = "0"
dialoguer = "0"
thiserror = "1"
ferinth = "1"
online = "3"
bytes = "1"
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ use util::{
};

#[tokio::main]
async fn main() -> FResult<()> {
async fn main() {
if let Some(err) = actual_main().await.err() {
println!("{}", err);
std::process::exit(1);
}
}

async fn actual_main() -> FResult<()> {
// Get the command to execute from Clap
// This also displays help, version
let command = cli::get_subcommand()?;
Expand Down
96 changes: 29 additions & 67 deletions src/util/ferium_error.rs
Original file line number Diff line number Diff line change
@@ -1,86 +1,48 @@
use std::{
convert::From,
fmt::{Debug, Formatter},
};
use std::fmt::Debug;
use thiserror::Error;

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

#[derive(Error, Debug)]
pub enum FError {
/// The config file does not contain mods or repos
#[error("Your config file is empty! Run `ferium help` to see how to add mods or repositories")]
EmptyConfigFile,
/// An HTTP(S) request returned with an error
ReqwestError { error: reqwest::Error },
#[error("Failed to send/process an HTTP(S) request due to {}", .0)]
ReqwestError(#[from] reqwest::Error),
/// Failure to unwrap an Option, akin to `NullPointerError`s
#[error("Could not access an expected value")]
OptionError,
/// Failed to parse a regular expression
RegexError,
#[error("Failed to parse regular expression")]
RegexError(#[from] fancy_regex::Error),
/// A JSON error occured
JsonError {
category: serde_json::error::Category,
},
#[error("{}", match .0.classify() {
serde_json::error::Category::Syntax => {
"Syntax error encountered in JSON file"
},
serde_json::error::Category::Data => {
"Non matching type while deserialising JSON"
},
serde_json::error::Category::Eof => {
"Unexpected end of file while reading JSON"
},
serde_json::error::Category::Io => {
"Encountered an Input/Output error while handling JSON"
},
})]
JsonError(#[from] serde_json::Error),
/// An HTTP(S) request encountered an error
#[error("An HTTP(S) request returned an error, {}", message)]
HTTPError { message: String },
/// An I/O error occured
IOError { description: String },
#[error("Encountered an input/output error, {}", .0.to_string())]
IOError(#[from] std::io::Error),
/// The program is running on an unsupported device
#[error("The device you are currently running on is unsupported by Ferium")]
InvalidDeviceError,
/// The application should print `message` and quit (gracefully)
#[error("{}", message)]
Quit { message: String },
}

impl Debug for FError {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), std::fmt::Error> {
match self {
FError::EmptyConfigFile => write!(fmt, "Your config file is empty! Run `ferium help` to see how to add mods or repositories"),
FError::HTTPError { message } => write!(fmt, "An HTTP(S) request returned an error, {}", message),
FError::InvalidDeviceError => write!(fmt, "The device you are currently running on is unsupported by Ferium"),
FError::IOError {description} => write!(fmt, "Encountered an input/output error, {}", description),
FError::JsonError { category } => match category {
serde_json::error::Category::Syntax => {
write!(fmt, "Syntax error encountered in JSON file")
},
serde_json::error::Category::Data => {
write!(fmt, "Non matching type while deserialising JSON")
},
serde_json::error::Category::Eof => {
write!(fmt, "Unexpected end of file while reading JSON")
},
serde_json::error::Category::Io => {
write!(fmt, "Encountered an Input/Output error while handling JSON")
},
},
FError::OptionError => write!(fmt, "Could not access an expected value"),
FError::Quit { message } => write!(fmt, "{}", message),
FError::RegexError => write!(fmt, "Failed to parse regular expression"),
FError::ReqwestError { error }=> write!(fmt, "Failed to send/process an HTTP(S) request due to {}", error),
}
}
}

impl From<reqwest::Error> for FError {
fn from(err: reqwest::Error) -> Self {
Self::ReqwestError { error: err }
}
}

impl From<fancy_regex::Error> for FError {
fn from(_: fancy_regex::Error) -> Self {
Self::RegexError
}
}

impl From<std::io::Error> for FError {
fn from(err: std::io::Error) -> Self {
Self::IOError {
description: err.to_string(),
}
}
}

impl From<serde_json::Error> for FError {
fn from(err: serde_json::Error) -> Self {
Self::JsonError {
category: err.classify(),
}
}
}

0 comments on commit 1cec37a

Please sign in to comment.