From 507757d227aa00874a2d33caed1a78c5b8cc7132 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 18 Jun 2020 01:56:20 +0200 Subject: add some more context to some errors Some error messages currently look very weird. For example, if the given configuration file does not exist, it gives you an error about a missing file - which could also be the file to upload though, as the error doesn't specify. Therefore, some more context for the error messages is nice. The "sourceback" could still use some work. --- src/main.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index f7f4bac..f2fa2ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ use std::{ time::Duration, }; -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, Context, Result}; use clap::Clap; use evtclib::{Boss, Compression, Log}; use log::{debug, error, info, warn}; @@ -62,12 +62,15 @@ fn main() { let opts = Opts::parse(); if let Err(e) = inner_main(&opts) { error!("{}", e); + e.chain() + .skip(1) + .for_each(|cause| error!("... because: {}", cause)); std::process::exit(1); } } fn inner_main(opts: &Opts) -> Result<()> { - let config = config::load(&opts.config)?; + let config = config::load(&opts.config).context("Could not load configuration")?; match &opts.subcmd { SubCommand::Watch(w) => watch(w, &config)?, SubCommand::Upload(u) => { @@ -75,7 +78,8 @@ fn inner_main(opts: &Opts) -> Result<()> { println!("{}", permalink); if let Some(d) = &config.discord { let log = load_log(&u.path)?; - discord::post_link(&d.auth_token, d.channel_id, log, permalink)?; + discord::post_link(&d.auth_token, d.channel_id, log, permalink) + .context("Could not post link to Discord")?; } } } @@ -88,7 +92,9 @@ fn watch(watch: &Watch, config: &Config) -> Result<()> { let (tx, rx) = channel(); let mut watcher = notify::watcher(tx, Duration::from_secs(WATCH_DELAY_SECONDS))?; - watcher.watch(&watch.dirname, RecursiveMode::Recursive)?; + watcher + .watch(&watch.dirname, RecursiveMode::Recursive) + .context("Could not watch the given directory")?; info!("Watcher set up, watching {:?}", watch.dirname); loop { let event = rx.recv()?; @@ -159,7 +165,8 @@ fn handle_file(config: &Config, filename: &Path) -> Result<()> { info!("Uploaded log, available at {}", permalink); if let Some(d) = &config.discord { - discord::post_link(&d.auth_token, d.channel_id, log, permalink)?; + discord::post_link(&d.auth_token, d.channel_id, log, permalink) + .context("Could not post link to Discord")?; info!("Posted link to Discord"); } -- cgit v1.2.3