Trait UFE

Source
pub trait UFE: Error {
    // Provided methods
    fn summary(&self) -> String { ... }
    fn reasons(&self) -> Option<Vec<String>> { ... }
    fn helptext(&self) -> Option<String> { ... }
    fn print(&self) { ... }
    fn print_and_exit(&self) { ... }
    fn into_ufe(&self) -> UserFacingError { ... }
}
Expand description

You can implement UFE on your error types pretty print them. The default implementation will print Error: <your error .to_string()> followed by a list of reasons that are any errors returned by .source(). You should only override the summary, reasons and help text functions. The pretty print versions of these are used by print(), print_and_exit() and contain the formatting. If you wish to change the formatting you should update it with the formatting functions.

Provided Methods§

Source

fn summary(&self) -> String

Returns a summary of the error. This will be printed in red, prefixed by “Error: “, at the top of the error message.

Source

fn reasons(&self) -> Option<Vec<String>>

Returns a vector of Strings that will be listed as bullet points below the summary. By default, lists any errors returned by .source() recursively.

Source

fn helptext(&self) -> Option<String>

Returns help text that is listed below the reasons in a muted fashion. Useful for additional details, or suggested next steps.

Source

fn print(&self)

Prints the formatted error.

§Example
use user_error::{UserFacingError, UFE};
UserFacingError::new("File failed to open")
        .reason("File not found")
        .help("Try: touch file.txt")
        .print();
Source

fn print_and_exit(&self)

Convenience function that pretty prints the error and exits the program.

§Example
use user_error::{UserFacingError, UFE};
UserFacingError::new("File failed to open")
        .reason("File not found")
        .help("Try: touch file.txt")
        .print_and_exit();
Source

fn into_ufe(&self) -> UserFacingError

Consumes the UFE and returns a UserFacingError. Useful if you want access to additional functions to edit the error message before exiting the program.

§Example
use user_error::{UserFacingError, UFE};
use std::fmt::{self, Display};
use std::error::Error;

#[derive(Debug)]
struct MyError {}

impl Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "MyError")
    }
}

impl Error for MyError {
    fn source(&self) -> Option<&(dyn Error + 'static)> { None }
}

impl UFE for MyError {}

fn main() {
    let me = MyError {};
    me.print();
    me.into_ufe()
        .help("Added help text")
        .print();
}

Implementors§