Skip to content

Commit f35bb94

Browse files
author
Marek Suchánek
committed
Improve error handling
1 parent 1d85e47 commit f35bb94

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![forbid(unsafe_code)]
1212

1313
use clap::ArgMatches;
14-
use color_eyre::eyre::Result;
14+
use color_eyre::eyre::{bail, eyre, Result, WrapErr};
1515

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

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

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

src/validation.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,12 @@ mod title {
291291

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

295-
if attribute.get(1).unwrap().as_str() == "context" {
299+
if attribute_name == "context" {
296300
// The context attribute is allowed
297301
None
298302
} else {

src/write.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fs;
22
use std::io;
33
use std::path::PathBuf;
44

5-
use color_eyre::eyre::{Context, Result};
5+
use color_eyre::eyre::{eyre, Result, WrapErr};
66

77
use crate::module::Module;
88
use crate::Options;
@@ -26,7 +26,7 @@ impl Module {
2626

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

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

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

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

0 commit comments

Comments
 (0)