Skip to content

Commit d8796dc

Browse files
authored
Rollup merge of rust-lang#83780 - matklad:doc-error-message, r=JohnTitor
Document "standard" conventions for error messages These are currently documented in the API guidelines: https://rust-lang.github.io/api-guidelines/interoperability.html#error-types-are-meaningful-and-well-behaved-c-good-err I think it makes sense to uplift this guideline (in a milder form) into std docs. Printing and producing errors is something that even non-expert users do frequently, so it is useful to give at least some indication of what a typical error message looks like.
2 parents 8ab4ce2 + 5886fe6 commit d8796dc

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

std/src/error.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,22 @@ use crate::string;
3333
use crate::sync::Arc;
3434

3535
/// `Error` is a trait representing the basic expectations for error values,
36-
/// i.e., values of type `E` in [`Result<T, E>`]. Errors must describe
37-
/// themselves through the [`Display`] and [`Debug`] traits, and may provide
38-
/// cause chain information:
36+
/// i.e., values of type `E` in [`Result<T, E>`].
3937
///
40-
/// [`Error::source()`] is generally used when errors cross
41-
/// "abstraction boundaries". If one module must report an error that is caused
42-
/// by an error from a lower-level module, it can allow accessing that error
43-
/// via [`Error::source()`]. This makes it possible for the high-level
44-
/// module to provide its own errors while also revealing some of the
38+
/// Errors must describe themselves through the [`Display`] and [`Debug`]
39+
/// traits. Error messages are typically concise lowercase sentences without
40+
/// trailing punctuation:
41+
///
42+
/// ```
43+
/// let err = "NaN".parse::<u32>().unwrap_err();
44+
/// assert_eq!(err.to_string(), "invalid digit found in string");
45+
/// ```
46+
///
47+
/// Errors may provide cause chain information. [`Error::source()`] is generally
48+
/// used when errors cross "abstraction boundaries". If one module must report
49+
/// an error that is caused by an error from a lower-level module, it can allow
50+
/// accessing that error via [`Error::source()`]. This makes it possible for the
51+
/// high-level module to provide its own errors while also revealing some of the
4552
/// implementation for debugging via `source` chains.
4653
#[stable(feature = "rust1", since = "1.0.0")]
4754
pub trait Error: Debug + Display {

0 commit comments

Comments
 (0)