Wrapping errors - Rust By Example
Description: A description

# Wrapping errors

An alternative to boxing errors is to wrap them in your own error type.

    use std::error;
    use std::num::ParseIntError;
    use std::fmt;

    type Result<T> = std::result::Result<T, DoubleError>;

    #[derive(Debug)]
    enum DoubleError {
        EmptyVec,
        // We will defer to the parse error implementation for their error.
        // Supplying extra info requires adding more data to the type.
        Parse(ParseIntError),
    }

    impl fmt::Display for DoubleError {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            match *self {
                DoubleError::EmptyVec =>
                    write!(f, "please use a vector with at least one element"),
                // This is a wrapper, so defer to the underlying types' implementation of `fmt`.
                DoubleError::Parse(ref e) => e.fmt(f),
            }
        }
    }

    impl error::Error for DoubleError {
        fn source(&self) -> Option<&(dyn error::Error + 'static)> {
            match *self {
                DoubleError::EmptyVec => None,
                // The cause is the underlying implementation error type. Is implicitly
                // cast to the trait object `&error::Error`. This works because the
                // underlying type already implements the `Error` trait.
                DoubleError::Parse(ref e) => Some(e),
            }
        }
    }

    // Implement the conversion from `ParseIntError` to `DoubleError`.
    // This will be automatically called by `?` if a `ParseIntError`
    // needs to be converted into a `DoubleError`.
    impl From<ParseIntError> for DoubleError {
        fn from(err: ParseIntError) -> DoubleError {
            DoubleError::Parse(err)
        }
    }

    fn double_first(vec: Vec<&str>) -> Result<i32> {
        let first = vec.first().ok_or(DoubleError::EmptyVec)?;
        let parsed = first.parse::<i32>()?;

        Ok(2 * parsed)
    }

    fn print(result: Result<i32>) {
        match result {
            Ok(n)  => println!("The first doubled is {}", n),
            Err(e) => println!("Error: {}", e),
        }
    }

    fn main() {
        let numbers = vec!["42", "93", "18"];
        let empty = vec![];
        let strings = vec!["tofu", "93", "18"];

        print(double_first(numbers));
        print(double_first(empty));
        print(double_first(strings));
    }

This adds a bit more boilerplate for handling errors and might not be needed in all applications. There are some libraries that can take care of the boilerplate for you.

### See also:

`From::from` and `Enums`