Skip to main content

ResultExt

Trait ResultExt 

Source
pub trait ResultExt<T> {
    // Required methods
    fn context(
        self,
        kind: ErrorKind,
        context: impl Into<Cow<'static, str>>,
    ) -> Result<T>;
    fn kind(self, kind: ErrorKind) -> Result<T>;
}
Expand description

Adds a semantic kind and optional context when converting an external error.

Required Methods§

Source

fn context( self, kind: ErrorKind, context: impl Into<Cow<'static, str>>, ) -> Result<T>

Converts an external error into a service error with kind and human-readable context.

The source error is retained, while the rendered service error contains the semantic kind and context.

use objectstore_service::error::{ErrorKind, ResultExt as _};

let result = std::fs::read("missing")
    .context(ErrorKind::BackendFailure, "reading local object");
let error = result.unwrap_err();
assert_eq!(
    error.to_string(),
    "backend operation failed: reading local object"
);
Source

fn kind(self, kind: ErrorKind) -> Result<T>

Converts an external error into a service error with only kind.

Use this when the source already identifies the failure or when the operation is expected to be infallible. The source error is still retained.

use objectstore_service::error::{ErrorKind, ResultExt as _};

let result = "invalid".parse::<u64>().kind(ErrorKind::InvalidMetadata);
assert_eq!(result.unwrap_err().to_string(), "invalid object metadata");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T, E> ResultExt<T> for Result<T, E>
where E: StdError + Send + Sync + 'static,

Source§

fn context( self, kind: ErrorKind, context: impl Into<Cow<'static, str>>, ) -> Result<T>

Source§

fn kind(self, kind: ErrorKind) -> Result<T>

Implementors§