actually good errors now

This commit is contained in:
askiiart 2025-05-15 22:24:19 -05:00
parent 7315c2b154
commit 3f622cda5b
Signed by untrusted user who does not match committer: askiiart
GPG key ID: 6A32977DAF31746A
4 changed files with 33 additions and 29 deletions

21
Cargo.lock generated
View file

@ -242,6 +242,7 @@ dependencies = [
"clap_complete", "clap_complete",
"serde", "serde",
"serde_yml", "serde_yml",
"thiserror",
] ]
[[package]] [[package]]
@ -261,6 +262,26 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.18" version = "1.0.18"

View file

@ -9,6 +9,7 @@ clap = { version = "4.5.38", features = ["derive"] }
clap_complete = "*" clap_complete = "*"
serde = { version = "1.0.219", features = ["derive"] } serde = { version = "1.0.219", features = ["derive"] }
serde_yml = "0.0.12" serde_yml = "0.0.12"
thiserror = "2.0.12"
[build-dependencies] [build-dependencies]
clap = "*" clap = "*"

View file

@ -1,19 +1,10 @@
use std::fmt; use std::{io, path::PathBuf};
use thiserror::Error;
pub struct Error { #[derive(Error, Debug)]
pub message: String, pub enum Error {
} #[error("DeserializationError: failed to deserialize {0}\n{1}")]
DeserializationError(PathBuf, serde_yml::Error),
// Implement std::fmt::Display for Error #[error("IoError: {0}")]
impl fmt::Display for Error { IoError(#[from] io::Error),
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message) // user-facing output
}
}
// Implement std::fmt::Debug for Error
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Error message: {}\n{{ file: {}, line: {} }}", self.message, file!(), line!()) // programmer-facing output
}
} }

View file

@ -28,11 +28,9 @@ fn main() {
config = conf; config = conf;
} }
Err(e) => { Err(e) => {
println!("{}", e); panic!("{}", e);
exit(1);
} }
} }
// = get_config_from_path(config_path);
println!("{:?}", config); println!("{:?}", config);
} }
@ -42,19 +40,12 @@ fn get_config_from_path(path: PathBuf) -> Result<Config, Error> {
Ok(f) => match serde_yml::from_reader(f) { Ok(f) => match serde_yml::from_reader(f) {
Ok(conf) => return Ok(conf), Ok(conf) => return Ok(conf),
Err(e) => { Err(e) => {
return Err(Error { return Err(Error::DeserializationError(path, e));
message: format!(
"*** Error deserializing config file ({:?}), it's likely deformed ***\n{}",
path, e
),
});
} }
}, },
Err(e) => { Err(e) => {
println!(""); println!("");
return Err(Error { return Err(Error::IoError(e));
message: format!("*** Error opening config file ({:?}) ***\n{}", path, e),
});
} }
} }
} }