Struct narrate::Error

source ·
pub struct Error { /* private fields */ }
Expand description

Wrapper around a dynamic error type with an optional help message.

Error works a lot like Box<dyn std::error::Error>, but with these differences:

  • Error requires that the error is Send, Sync, and 'static.
  • Error is represented as a narrow pointer — exactly one word in size instead of two.
  • Error may contain a help message in order to suggest further actions a user might take.

Implementations

Create a new error object from any error type.

The error type must be thread-safe and 'static, so that the Error will be as well.

Convert an anyhow::Error into an error object.

Due to the generic implementation of From for Error: we cannot add a From<anyhow::Error> impl. Use this instead.

Wrap the error value with additional context.

For attaching context to a Result as it is propagated, the ErrorWrap extension trait may be more convenient than this function.

The primary reason to use error.warp(...) instead of result.warp(...) via the ErrorWrap trait would be if the context needs to depend on some data held by the underlying error:

use narrate::Result;
use std::fs::File;
use std::path::Path;

struct ParseError {
    line: usize,
    column: usize,
}

fn parse_impl(file: File) -> Result<T, ParseError> {
    ...
}

pub fn parse(path: impl AsRef<Path>) -> Result<T> {
    let file = File::open(&path)?;
    parse_impl(file).map_err(|error| {
        let context = format!(
            "only the first {} lines of {} are valid",
            error.line, path.as_ref().display(),
        );
        narrate::Error::new(error).wrap(context)
    })
}

Returns true if E is the type held by this error object.

For wrapped errors, this method returns true if E matches the type of the context C or the type of the error on which the context has been attached.

Attempt to downcast the error object to a concrete type.

Downcast this error object by reference.

Example
// If the error was caused by redaction, then return a tombstone instead
// of the content.
match root_cause.downcast_ref::<DataStoreError>() {
    Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),
    None => Err(error),
}

Downcast this error object by mutable reference.

An iterator of the chain of source errors contained by this Error.

This iterator will visit every error in the cause chain of this error object, beginning with the error that this error object was created from.

Example
use narrate::Error;
use std::io;

pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {
    for cause in error.chain() {
        if let Some(io_error) = cause.downcast_ref::<io::Error>() {
            return Some(io_error.kind());
        }
    }
    None
}

The lowest level cause of this error — this error’s cause’s cause’s cause etc.

The root cause is the last error in the iterator produced by chain().

Get a reference to this error’s help message

👎Deprecated

Set this error’s help message to an owned String

👎Deprecated

Set this error’s help message to a static &str

Trait Implementations

Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
CLI application exit code
Converts to this type from the input type.
Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.