From b9e6f8823d0a714d3f7ff2cb50ba6781236bf6c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Tue, 6 Dec 2022 18:13:49 +0100 Subject: [PATCH] fix: improve error handling for aoc-cli errors --- README.md | 2 +- src/lib.rs | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0b6e53f..869f57d 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ To read inputs for previous years, append the `--year/-y` flag. _(example: `carg ### Download puzzle inputs via aoc-cli -1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.6.0 +1. Install [`aoc-cli`](https://github.com/scarvalhojr/aoc-cli/) via cargo: `cargo install aoc-cli --version 0.7.0` 2. Create an `.adventofcode.session` file in your home directory and paste your session cookie[^1] into it. To get this, press F12 anywhere on the Advent of Code website to open your browser developer tools. Look in your Cookies under the Application or Storage tab, and copy out the `session` cookie value. Once installed, you can use the [download command](#download-input-for-a-day). diff --git a/src/lib.rs b/src/lib.rs index ed58a10..09a7ae3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,16 +133,18 @@ pub mod aoc_cli { pub enum AocCliError { CommandNotFound, - CommandFailed, + CommandNotCallable, + BadExitStatus(Output), IoError, } impl Display for AocCliError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - AocCliError::CommandNotFound => write!(f, "CommandNotFound"), - AocCliError::CommandFailed => write!(f, "CommandFailed"), - AocCliError::IoError => write!(f, "IoError"), + AocCliError::CommandNotFound => write!(f, "aoc-cli is not present in environment."), + AocCliError::CommandNotCallable => write!(f, "aoc-cli could not be called."), + AocCliError::BadExitStatus(_) => write!(f, "aoc-cli exited with a non-zero status."), + AocCliError::IoError => write!(f, "could not write output files to file system."), } } } @@ -181,11 +183,16 @@ pub mod aoc_cli { ); let output = call_aoc_cli(&args)?; - println!("---"); - println!("🎄 Successfully wrote input to \"{}\".", &input_path); - println!("🎄 Successfully wrote puzzle to \"{}\".", &puzzle_path); - Ok(output) + if output.status.success() { + println!("---"); + println!("🎄 Successfully wrote input to \"{}\".", &input_path); + println!("🎄 Successfully wrote puzzle to \"{}\".", &puzzle_path); + Ok(output) + } else { + Err(AocCliError::BadExitStatus(output)) + } + } fn get_input_path(day: u8) -> String { @@ -221,6 +228,6 @@ pub mod aoc_cli { .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .output() - .map_err(|_| AocCliError::CommandFailed) + .map_err(|_| AocCliError::CommandNotCallable) } }