actually good errors now
This commit is contained in:
parent
7315c2b154
commit
3f622cda5b
4 changed files with 33 additions and 29 deletions
21
Cargo.lock
generated
21
Cargo.lock
generated
|
@ -242,6 +242,7 @@ dependencies = [
|
|||
"clap_complete",
|
||||
"serde",
|
||||
"serde_yml",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -261,6 +262,26 @@ dependencies = [
|
|||
"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]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.18"
|
||||
|
|
|
@ -9,6 +9,7 @@ clap = { version = "4.5.38", features = ["derive"] }
|
|||
clap_complete = "*"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_yml = "0.0.12"
|
||||
thiserror = "2.0.12"
|
||||
|
||||
[build-dependencies]
|
||||
clap = "*"
|
||||
|
|
25
src/error.rs
25
src/error.rs
|
@ -1,19 +1,10 @@
|
|||
use std::fmt;
|
||||
use std::{io, path::PathBuf};
|
||||
use thiserror::Error;
|
||||
|
||||
pub struct Error {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
// Implement std::fmt::Display for Error
|
||||
impl fmt::Display for 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
|
||||
}
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("DeserializationError: failed to deserialize {0}\n{1}")]
|
||||
DeserializationError(PathBuf, serde_yml::Error),
|
||||
#[error("IoError: {0}")]
|
||||
IoError(#[from] io::Error),
|
||||
}
|
||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -28,11 +28,9 @@ fn main() {
|
|||
config = conf;
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{}", e);
|
||||
exit(1);
|
||||
panic!("{}", e);
|
||||
}
|
||||
}
|
||||
// = get_config_from_path(config_path);
|
||||
|
||||
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(conf) => return Ok(conf),
|
||||
Err(e) => {
|
||||
return Err(Error {
|
||||
message: format!(
|
||||
"*** Error deserializing config file ({:?}), it's likely deformed ***\n{}",
|
||||
path, e
|
||||
),
|
||||
});
|
||||
return Err(Error::DeserializationError(path, e));
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
println!("");
|
||||
return Err(Error {
|
||||
message: format!("*** Error opening config file ({:?}) ***\n{}", path, e),
|
||||
});
|
||||
return Err(Error::IoError(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue