[][src]Trait user_error::UFE

pub trait UFE: Error {
    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 { ... } }

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

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.

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.

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.

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();

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();

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();
}
Loading content...

Implementors

impl UFE for UserFacingError[src]

Loading content...