Skip to content

Commit b1a2ab8

Browse files
committed
Auto merge of rust-lang#38179 - michael-zapata:rf/harmonise_rustdoc_errors, r=GuillaumeGomez
feat(rustdoc): harmonise error messages Based on unix tools wording, it follows a standard format: `program_name: context: error message`, potentially prompting the user to use the `--help` option. This is clearly meant to trigger some discussion on rust-lang#38084, as messages still use `stdout` and `stderr` somewhat arbitrarily, and there are a few `error!()` calls as well.
2 parents 5a2b50b + 430d39d commit b1a2ab8

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

Diff for: src/librustdoc/lib.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ extern crate serialize as rustc_serialize; // used by deriving
5454
use std::collections::{BTreeMap, BTreeSet};
5555
use std::default::Default;
5656
use std::env;
57+
use std::fmt::Display;
58+
use std::io;
59+
use std::io::Write;
5760
use std::path::PathBuf;
5861
use std::process;
5962
use std::sync::mpsc::channel;
@@ -183,7 +186,7 @@ pub fn main_args(args: &[String]) -> isize {
183186
let matches = match getopts::getopts(&args[1..], &all_groups) {
184187
Ok(m) => m,
185188
Err(err) => {
186-
println!("{}", err);
189+
print_error(err);
187190
return 1;
188191
}
189192
};
@@ -211,11 +214,11 @@ pub fn main_args(args: &[String]) -> isize {
211214
}
212215

213216
if matches.free.is_empty() {
214-
println!("expected an input file to act on");
217+
print_error("missing file operand");
215218
return 1;
216219
}
217220
if matches.free.len() > 1 {
218-
println!("only one input file may be specified");
221+
print_error("too many file operands");
219222
return 1;
220223
}
221224
let input = &matches.free[0];
@@ -227,7 +230,7 @@ pub fn main_args(args: &[String]) -> isize {
227230
let externs = match parse_externs(&matches) {
228231
Ok(ex) => ex,
229232
Err(err) => {
230-
println!("{}", err);
233+
print_error(err);
231234
return 1;
232235
}
233236
};
@@ -247,14 +250,16 @@ pub fn main_args(args: &[String]) -> isize {
247250

248251
if let Some(ref p) = css_file_extension {
249252
if !p.is_file() {
250-
println!("{}", "--extend-css option must take a css file as input");
253+
writeln!(
254+
&mut io::stderr(),
255+
"rustdoc: option --extend-css argument must be a file."
256+
).unwrap();
251257
return 1;
252258
}
253259
}
254260

255261
let external_html = match ExternalHtml::load(
256-
&matches.opt_strs("html-in-header"),
257-
&matches.opt_strs("html-before-content"),
262+
&matches.opt_strs("html-in-header"), &matches.opt_strs("html-before-content"),
258263
&matches.opt_strs("html-after-content")) {
259264
Some(eh) => eh,
260265
None => return 3
@@ -291,17 +296,26 @@ pub fn main_args(args: &[String]) -> isize {
291296
0
292297
}
293298
Some(s) => {
294-
println!("unknown output format: {}", s);
299+
print_error(format!("unknown output format: {}", s));
295300
1
296301
}
297302
}
298303
});
299304
res.unwrap_or_else(|s| {
300-
println!("input error: {}", s);
305+
print_error(format!("input error: {}", s));
301306
1
302307
})
303308
}
304309

310+
/// Prints an uniformised error message on the standard error output
311+
fn print_error<T>(error_message: T) where T: Display {
312+
writeln!(
313+
&mut io::stderr(),
314+
"rustdoc: {}\nTry 'rustdoc --help' for more information.",
315+
error_message
316+
).unwrap();
317+
}
318+
305319
/// Looks inside the command line arguments to extract the relevant input format
306320
/// and files and then generates the necessary rustdoc output for formatting.
307321
fn acquire_input<R, F>(input: &str,

Diff for: src/librustdoc/markdown.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
7171
let mut out = match File::create(&output) {
7272
Err(e) => {
7373
let _ = writeln!(&mut io::stderr(),
74-
"error opening `{}` for writing: {}",
74+
"rustdoc: {}: {}",
7575
output.display(), e);
7676
return 4;
7777
}
@@ -80,8 +80,10 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
8080

8181
let (metadata, text) = extract_leading_metadata(&input_str);
8282
if metadata.is_empty() {
83-
let _ = writeln!(&mut io::stderr(),
84-
"invalid markdown file: expecting initial line with `% ...TITLE...`");
83+
let _ = writeln!(
84+
&mut io::stderr(),
85+
"rustdoc: invalid markdown file: expecting initial line with `% ...TITLE...`"
86+
);
8587
return 5;
8688
}
8789
let title = metadata[0];
@@ -132,7 +134,7 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
132134
match err {
133135
Err(e) => {
134136
let _ = writeln!(&mut io::stderr(),
135-
"error writing to `{}`: {}",
137+
"rustdoc: cannot write to `{}`: {}",
136138
output.display(), e);
137139
6
138140
}

0 commit comments

Comments
 (0)