Skip to content

Commit

Permalink
Implements proper user error handling (#5)
Browse files Browse the repository at this point in the history
* Implements proper user error handling

We deliniate between user errors and program errors.
Any user error should effectively cascasde up to where it's relevant. We
don't want it crashing the program or anything like that either.

What I've done here is expand on the existing use of the UFE library
(user facing errors) which gives us nice rendering and the ability to
trivially define errors, the reason for them, and how you might fix
them.

We combined this with the macro based error library to make easy to use
errors for devs with in theory meaningful output for users.

There's a lot of stuff that should error that just doesn't (configs) but
I think this is a lot better then how things were before.

There are a few issues, mostly with converting out of crate errors into
this system. It's less a matter of being impossible and more I don't
have the kahones to setup hints for every single type on both input and
output failures. So we just let them speak for themselves.

Anyhow tool should crash less and report more now, which is pog.

Oh also I've pulled in owo-colors to make the terminal output a bit
more pretty.
  • Loading branch information
LemonInTheDark authored Apr 29, 2024
1 parent 7343818 commit 3eb24cd
Show file tree
Hide file tree
Showing 20 changed files with 468 additions and 106 deletions.
2 changes: 0 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]

[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=+crt-static"]
145 changes: 131 additions & 14 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions hypnagogic_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tracing-subscriber = "0.3"
user-error ="1.2"
walkdir = "2.3"
hypnagogic-core = { path = "../hypnagogic_core" }
owo-colors = { version = "4.0.0", features = ["supports-colors"] }

[dev-dependencies]
tempfile = "3.5"
Expand Down
14 changes: 14 additions & 0 deletions hypnagogic_cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::io;
use std::path::PathBuf;

use hypnagogic_core::config::error::ConfigError;
use hypnagogic_core::operations::error::ProcessorError;
use hypnagogic_core::operations::{InputError, OutputError};
use thiserror::Error;
use user_error::UFE;

Expand All @@ -24,6 +26,12 @@ pub enum Error {
template_string: String,
expected_path: PathBuf,
},
#[error("Image Parsing Failed")]
InputParsingFailed(#[from] InputError),
#[error("Processing Failed")]
ProcessorFailed(#[from] ProcessorError),
#[error("Output Failed")]
OutputWriteFailed(#[from] OutputError),
#[error("No template folder")]
NoTemplateFolder(PathBuf),
#[error("Generic IO Error")]
Expand Down Expand Up @@ -74,6 +82,9 @@ impl UFE for Error {
format!("Expected template folder at {folder:?}"),
])
}
Error::InputParsingFailed(image_error) => image_error.reasons(),
Error::ProcessorFailed(process_error) => process_error.reasons(),
Error::OutputWriteFailed(output_error) => output_error.reasons(),
Error::IO(err) => {
Some(vec![format!(
"Operation failed for reason of \"{:?}\"",
Expand Down Expand Up @@ -110,6 +121,9 @@ impl UFE for Error {
.to_string(),
)
}
Error::InputParsingFailed(image_error) => image_error.helptext(),
Error::ProcessorFailed(process_error) => process_error.helptext(),
Error::OutputWriteFailed(output_error) => output_error.helptext(),
Error::IO(_) => {
Some(
"Make sure the directories or files aren't in use, and you have permission to \
Expand Down
Loading

0 comments on commit 3eb24cd

Please sign in to comment.