Improve error reports somewhat

feature/lookup-installed
trivernis 1 year ago
parent d63d9d8d55
commit bf750af4cf
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: DFFFCC2C7A02DB45

@ -16,6 +16,7 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error, Diagnostic)] #[derive(Debug, Error, Diagnostic)]
pub enum Error { pub enum Error {
#[diagnostic(code(nenv::web))]
#[error("Failed to call nodejs.com api.")] #[error("Failed to call nodejs.com api.")]
Web( Web(
#[from] #[from]
@ -24,6 +25,7 @@ pub enum Error {
ApiError, ApiError,
), ),
#[diagnostic(code(nenv::extract))]
#[error("The node archive could not be extracted")] #[error("The node archive could not be extracted")]
Extract( Extract(
#[from] #[from]
@ -32,14 +34,15 @@ pub enum Error {
ExtractError, ExtractError,
), ),
#[diagnostic(code(nenv::config))]
#[error("The config file could not be loaded")] #[error("The config file could not be loaded")]
Config( Config(
#[from] #[from]
#[source]
#[diagnostic_source] #[diagnostic_source]
ConfigError, ConfigError,
), ),
#[diagnostic(code(nenv::mapper))]
#[error("Mapping failed")] #[error("Mapping failed")]
Mapper( Mapper(
#[from] #[from]
@ -48,6 +51,7 @@ pub enum Error {
MapperError, MapperError,
), ),
#[diagnostic(code(nenv::version))]
#[error("The passed version is invalid")] #[error("The passed version is invalid")]
Version( Version(
#[from] #[from]
@ -55,9 +59,11 @@ pub enum Error {
VersionError, VersionError,
), ),
#[diagnostic(code(nenv::json))]
#[error("Failed to work with json")] #[error("Failed to work with json")]
Json(#[from] serde_json::Error), Json(#[from] serde_json::Error),
#[diagnostic(code(nenv::io))]
#[error("Error during IO operation")] #[error("Error during IO operation")]
Io(#[from] io::Error), Io(#[from] io::Error),
} }

@ -7,6 +7,7 @@ mod args;
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() -> miette::Result<()> { async fn main() -> miette::Result<()> {
miette::set_panic_hook();
let args: Args = Args::parse(); let args: Args = Args::parse();
match args.commmand { match args.commmand {

@ -1,6 +1,6 @@
use std::io; use std::io;
use miette::Diagnostic; use miette::{Diagnostic, NamedSource, SourceSpan};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
use tokio::fs; use tokio::fs;
@ -20,18 +20,27 @@ pub type ConfigResult<T> = Result<T, ConfigError>;
#[derive(Error, Diagnostic, Debug)] #[derive(Error, Diagnostic, Debug)]
pub enum ConfigError { pub enum ConfigError {
#[diagnostic(code(nenv::config::io))]
#[error("IO Error: {0}")] #[error("IO Error: {0}")]
Io( Io(
#[from] #[from]
#[source] #[source]
io::Error, io::Error,
), ),
#[error("Failed to parse config file: {0}")] #[diagnostic(code(nenv::config::parse))]
Parse( #[error("Failed to parse config file")]
#[from] #[diagnostic_source]
Parse {
#[source_code]
src: NamedSource,
#[label]
pos: Option<(usize, usize)>,
#[source] #[source]
toml::de::Error, e: toml::de::Error,
), },
#[diagnostic(code(nenv::config::write))]
#[error("Failed to serialize config file: {0}")] #[error("Failed to serialize config file: {0}")]
Serialize( Serialize(
#[from] #[from]
@ -62,7 +71,11 @@ impl Config {
Ok(cfg) Ok(cfg)
} else { } else {
let cfg_string = fs::read_to_string(&*CFG_FILE_PATH).await?; let cfg_string = fs::read_to_string(&*CFG_FILE_PATH).await?;
let cfg = toml::from_str(&cfg_string)?; let cfg = toml::from_str(&cfg_string).map_err(|e| ConfigError::Parse {
src: NamedSource::new("config.toml", cfg_string),
pos: e.line_col(),
e,
})?;
Ok(cfg) Ok(cfg)
} }

Loading…
Cancel
Save