Skip to content

Commit 80fb5ca

Browse files
committed
Handle errors properly in rustbook.
Silently ignoring errors is :( so lets not silently ignore them. huon is :) now.
1 parent 4247a30 commit 80fb5ca

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

src/rustbook/book.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub fn parse_summary<R: Reader>(input: R, src: &Path) -> Result<Book, Vec<String
124124
let path_from_root = match src.join(given_path.unwrap()).path_relative_from(src) {
125125
Some(p) => p,
126126
None => {
127-
errors.push(format!("Paths in SUMMARY.md must be relative, \
127+
errors.push(format!("paths in SUMMARY.md must be relative, \
128128
but path '{}' for section '{}' is not.",
129129
given_path.unwrap(), title));
130130
Path::new("")
@@ -148,8 +148,9 @@ pub fn parse_summary<R: Reader>(input: R, src: &Path) -> Result<Book, Vec<String
148148
}).sum() / 4 + 1;
149149

150150
if level > stack.len() + 1 {
151-
// FIXME: better error message
152-
errors.push(format!("Section '{}' is indented too many levels.", item.title));
151+
errors.push(format!("section '{}' is indented too deeply; \
152+
found {}, expected {} or less",
153+
item.title, level, stack.len() + 1));
153154
} else if level <= stack.len() {
154155
collapse(&mut stack, &mut top_items, level);
155156
}

src/rustbook/build.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ fn write_toc(book: &Book, path_to_root: &Path, out: &mut Writer) -> IoResult<()>
7373
}
7474

7575
fn render(book: &Book, tgt: &Path) -> CliResult<()> {
76-
let tmp = TempDir::new("rust-book")
77-
.ok()
78-
// FIXME: lift to Result instead
79-
.expect("could not create temporary directory");
76+
let tmp = try!(TempDir::new("rust-book"));
8077

8178
for (section, item) in book.iter() {
8279
println!("{} {}", section, item.title);
@@ -163,30 +160,24 @@ impl Subcommand for Build {
163160
tgt = Path::new(os::args()[3].clone());
164161
}
165162

166-
let _ = fs::mkdir(&tgt, io::USER_DIR); // FIXME: handle errors
163+
try!(fs::mkdir(&tgt, io::USER_DIR));
167164

168-
// FIXME: handle errors
169-
let _ = File::create(&tgt.join("rust-book.css")).write_str(css::STYLE);
165+
try!(File::create(&tgt.join("rust-book.css")).write_str(css::STYLE));
170166

171-
let summary = File::open(&src.join("SUMMARY.md"));
167+
let summary = try!(File::open(&src.join("SUMMARY.md")));
172168
match book::parse_summary(summary, &src) {
173169
Ok(book) => {
174170
// execute rustdoc on the whole book
175-
try!(render(&book, &tgt).map_err(|err| {
176-
term.err(&format!("error: {}", err.description())[]);
177-
err.detail().map(|detail| {
178-
term.err(&format!("detail: {}", detail)[]);
179-
});
180-
err
181-
}))
171+
render(&book, &tgt)
182172
}
183173
Err(errors) => {
174+
let n = errors.len();
184175
for err in errors.into_iter() {
185-
term.err(&err[]);
176+
term.err(&format!("error: {}", err)[]);
186177
}
178+
179+
Err(box format!("{} errors occurred", n) as Box<Error>)
187180
}
188181
}
189-
190-
Ok(()) // lol
191182
}
192183
}

src/rustbook/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ impl Error for IoError {
7979
}
8080
}
8181

82+
8283
//fn iter_map_err<T, U, E, I: Iterator<Result<T,E>>>(iter: I,

src/rustbook/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ fn main() {
5454
Ok(_) => {
5555
match subcmd.execute(&mut term) {
5656
Ok(_) => (),
57-
Err(_) => os::set_exit_status(-1),
57+
Err(err) => {
58+
term.err(&format!("error: {}", err.description())[]);
59+
err.detail().map(|detail| {
60+
term.err(&format!("detail: {}", detail)[]);
61+
});
62+
}
5863
}
5964
}
6065
Err(err) => {

0 commit comments

Comments
 (0)