1
Fork 0

Handle errors properly in rustbook.

Silently ignoring errors is :( so lets not silently ignore them. huon is
:) now.
This commit is contained in:
Huon Wilson 2015-01-09 20:25:45 +11:00
parent 4247a30bdd
commit 80fb5ca874
4 changed files with 20 additions and 22 deletions

View file

@ -124,7 +124,7 @@ pub fn parse_summary<R: Reader>(input: R, src: &Path) -> Result<Book, Vec<String
let path_from_root = match src.join(given_path.unwrap()).path_relative_from(src) { let path_from_root = match src.join(given_path.unwrap()).path_relative_from(src) {
Some(p) => p, Some(p) => p,
None => { None => {
errors.push(format!("Paths in SUMMARY.md must be relative, \ errors.push(format!("paths in SUMMARY.md must be relative, \
but path '{}' for section '{}' is not.", but path '{}' for section '{}' is not.",
given_path.unwrap(), title)); given_path.unwrap(), title));
Path::new("") Path::new("")
@ -148,8 +148,9 @@ pub fn parse_summary<R: Reader>(input: R, src: &Path) -> Result<Book, Vec<String
}).sum() / 4 + 1; }).sum() / 4 + 1;
if level > stack.len() + 1 { if level > stack.len() + 1 {
// FIXME: better error message errors.push(format!("section '{}' is indented too deeply; \
errors.push(format!("Section '{}' is indented too many levels.", item.title)); found {}, expected {} or less",
item.title, level, stack.len() + 1));
} else if level <= stack.len() { } else if level <= stack.len() {
collapse(&mut stack, &mut top_items, level); collapse(&mut stack, &mut top_items, level);
} }

View file

@ -73,10 +73,7 @@ fn write_toc(book: &Book, path_to_root: &Path, out: &mut Writer) -> IoResult<()>
} }
fn render(book: &Book, tgt: &Path) -> CliResult<()> { fn render(book: &Book, tgt: &Path) -> CliResult<()> {
let tmp = TempDir::new("rust-book") let tmp = try!(TempDir::new("rust-book"));
.ok()
// FIXME: lift to Result instead
.expect("could not create temporary directory");
for (section, item) in book.iter() { for (section, item) in book.iter() {
println!("{} {}", section, item.title); println!("{} {}", section, item.title);
@ -163,30 +160,24 @@ impl Subcommand for Build {
tgt = Path::new(os::args()[3].clone()); tgt = Path::new(os::args()[3].clone());
} }
let _ = fs::mkdir(&tgt, io::USER_DIR); // FIXME: handle errors try!(fs::mkdir(&tgt, io::USER_DIR));
// FIXME: handle errors try!(File::create(&tgt.join("rust-book.css")).write_str(css::STYLE));
let _ = File::create(&tgt.join("rust-book.css")).write_str(css::STYLE);
let summary = File::open(&src.join("SUMMARY.md")); let summary = try!(File::open(&src.join("SUMMARY.md")));
match book::parse_summary(summary, &src) { match book::parse_summary(summary, &src) {
Ok(book) => { Ok(book) => {
// execute rustdoc on the whole book // execute rustdoc on the whole book
try!(render(&book, &tgt).map_err(|err| { render(&book, &tgt)
term.err(&format!("error: {}", err.description())[]);
err.detail().map(|detail| {
term.err(&format!("detail: {}", detail)[]);
});
err
}))
} }
Err(errors) => { Err(errors) => {
let n = errors.len();
for err in errors.into_iter() { for err in errors.into_iter() {
term.err(&err[]); term.err(&format!("error: {}", err)[]);
} }
Err(box format!("{} errors occurred", n) as Box<Error>)
} }
} }
Ok(()) // lol
} }
} }

View file

@ -79,4 +79,5 @@ impl Error for IoError {
} }
} }
//fn iter_map_err<T, U, E, I: Iterator<Result<T,E>>>(iter: I, //fn iter_map_err<T, U, E, I: Iterator<Result<T,E>>>(iter: I,

View file

@ -54,7 +54,12 @@ fn main() {
Ok(_) => { Ok(_) => {
match subcmd.execute(&mut term) { match subcmd.execute(&mut term) {
Ok(_) => (), Ok(_) => (),
Err(_) => os::set_exit_status(-1), Err(err) => {
term.err(&format!("error: {}", err.description())[]);
err.detail().map(|detail| {
term.err(&format!("detail: {}", detail)[]);
});
}
} }
} }
Err(err) => { Err(err) => {