Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
msuchane committed Jul 29, 2022
1 parent 1d85e47 commit f35bb94
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#![forbid(unsafe_code)]

use clap::ArgMatches;
use color_eyre::eyre::Result;
use color_eyre::eyre::{bail, eyre, Result, WrapErr};

pub mod cmd_line;
mod logging;
Expand Down Expand Up @@ -114,8 +114,11 @@ pub fn run(options: &Options, cmdline_args: &ArgMatches) -> Result<()> {
.map(|module| module.include_statement)
.collect();

// The include_statements should never be empty thanks to the required group in clap
assert!(!include_statements.is_empty());
// The include_statements should never be empty thanks to the required group in clap.
// Make sure once more, though.
if include_statements.is_empty() {
bail!("The populated assembly includes no other files.");
}

// Generate the populated assembly module
let populated: Module = Input::new(ContentType::Assembly, title, options)
Expand All @@ -128,7 +131,8 @@ pub fn run(options: &Options, cmdline_args: &ArgMatches) -> Result<()> {
// Validate all file names specified on the command line
if let Some(files_iterator) = cmdline_args.values_of("validate") {
for file in files_iterator {
validation::validate(file)?;
validation::validate(file)
.wrap_err_with(|| eyre!("Failed to validate file {:?}", file))?;
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,12 @@ mod title {

let attribute_regex = Regex::new(r"\{((?:[[:alnum:]]|[-_])+)\}").expect(REGEX_ERROR);
let attribute = attribute_regex.captures(mod_id)?;
let attribute_name = attribute
.get(1)
.expect("Failed to extract an attribute name. Please report this as a bug")
.as_str();

if attribute.get(1).unwrap().as_str() == "context" {
if attribute_name == "context" {
// The context attribute is allowed
None
} else {
Expand Down
10 changes: 4 additions & 6 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs;
use std::io;
use std::path::PathBuf;

use color_eyre::eyre::{Context, Result};
use color_eyre::eyre::{eyre, Result, WrapErr};

use crate::module::Module;
use crate::Options;
Expand All @@ -26,7 +26,7 @@ impl Module {

io::stdin()
.read_line(&mut answer)
.context("Failed to read your response")?;
.wrap_err_with(|| eyre!("Failed to read your response: {:?}", answer))?;

match answer.trim().to_lowercase().as_str() {
"y" | "yes" => {
Expand All @@ -42,10 +42,8 @@ impl Module {
}

// If the target file doesn't exist, try to write to it
fs::write(full_path, &self.text).context(format!(
"Failed to write the `{}` file.",
&full_path.display()
))?;
fs::write(full_path, &self.text)
.wrap_err_with(|| eyre!("Failed to write the `{}` file.", &full_path.display()))?;

// If the write succeeds, print the include statement
log::debug!("Successfully written file `{}`", &full_path.display());
Expand Down

0 comments on commit f35bb94

Please sign in to comment.