Improve error reports somewhat

feature/lookup-installed
trivernis 2 years 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)]
pub enum Error {
#[diagnostic(code(nenv::web))]
#[error("Failed to call nodejs.com api.")]
Web(
#[from]
@ -24,6 +25,7 @@ pub enum Error {
ApiError,
),
#[diagnostic(code(nenv::extract))]
#[error("The node archive could not be extracted")]
Extract(
#[from]
@ -32,14 +34,15 @@ pub enum Error {
ExtractError,
),
#[diagnostic(code(nenv::config))]
#[error("The config file could not be loaded")]
Config(
#[from]
#[source]
#[diagnostic_source]
ConfigError,
),
#[diagnostic(code(nenv::mapper))]
#[error("Mapping failed")]
Mapper(
#[from]
@ -48,6 +51,7 @@ pub enum Error {
MapperError,
),
#[diagnostic(code(nenv::version))]
#[error("The passed version is invalid")]
Version(
#[from]
@ -55,9 +59,11 @@ pub enum Error {
VersionError,
),
#[diagnostic(code(nenv::json))]
#[error("Failed to work with json")]
Json(#[from] serde_json::Error),
#[diagnostic(code(nenv::io))]
#[error("Error during IO operation")]
Io(#[from] io::Error),
}

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

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

Loading…
Cancel
Save