Crate display_full_error

Source
Expand description

Display Full Error - Minimal display formatter for error chains

This library provides the DisplayFullError wrapper type to format errors with their chain of sources.

Error messages are formatted on a single line, separated with : ; up to 1024 messages per chain are printed, after which a single : ... is printed.

That’s all there is to it, there is no extra configuration or advanced features. This is intended as the most minimal formatter supporting error sources, to address the fact that there’s no helper in the standard library so far as of Rust 1.85 (2025-03). If a standard formatter supporting error sources is added, this crate will be deprecated (but remain available).

As a convenience, this library also exposes the DisplayFullErrorExt trait. It adds the display_full method to errors which returns the error in the formatting wrapper, as well as the to_string_full method as a shorthand for .display_full().to_string().

use ::core::{error, fmt};

use ::display_full_error::{DisplayFullError, DisplayFullErrorExt};

// main error
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum UploadError {
  Permission(PermissionError),
  Limit(LimitError),
}
impl fmt::Display for UploadError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.write_str("upload failed")
  }
}
impl error::Error for UploadError {
  fn source(&self) -> Option<&(dyn error::Error + 'static)> {
    Some(match self {
      UploadError::Permission(e) => e,
      UploadError::Limit(e) => e,
    })
  }
}

// first source error
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct PermissionError;
impl fmt::Display for PermissionError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.write_str("permission denied")
  }
}
impl error::Error for PermissionError {}

// second source error
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct LimitError;
impl fmt::Display for LimitError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.write_str("upload exceeds max limit")
  }
}
impl error::Error for LimitError {}

// usage example
let err = UploadError::Permission(PermissionError);

// You can use the wrapper directly, e.g. in a `format!`
assert_eq!(format!("the app crashed: {}", DisplayFullError(&err)), String::from("the app crashed: upload failed: permission denied"));
// Or you can use `to_string`
assert_eq!(DisplayFullError(&err).to_string(), String::from("upload failed: permission denied"));
// You can also use the convenience methods from the extension trait
assert_eq!(format!("the app crashed: {}", err.display_full()), String::from("the app crashed: upload failed: permission denied"));
// `to_string_full` requires the `alloc` feature to be enabled
#[cfg(feature = "alloc")]
assert_eq!(err.to_string_full(), String::from("upload failed: permission denied"));

This library requires Rust 1.81.0 or later as it depends on the Rust feature error_in_core. This library is compatible with no_std. There are no dependencies or optional features. This library does not introduce any runtime panics. It is recommended to use this library as an internal helper and to avoid leaking it into your public APIs. The output is guaranteed to be stable, any change would cause a major version bump.

The formatting uses : as it follows existing conventions and allows to keep the formatted error on a single line if the error messages don’t include newlines. Keeping the error on a single line increases compatibility with tools handling error output.

The maximum number of messages could have been a const parameter, but making it so currently harms ergonomics quite a lot as there is no support for default const values as of Rust 1.83. See the following Rust issues: #27336, #85077.

Structs§

DisplayFullError
Formatting wrapper to display errors, including their sources.

Constants§

MESSAGE_LIMIT
Maximum number of messages to print in a single full error.

Traits§

DisplayFullErrorExt
Extension trait providing convenience methods on errors.