pub trait SiftError<T, C>{
// Required methods
fn context(self, ctx: C) -> Result<T>;
fn with_context<F>(self, op: F) -> Result<T>
where F: Fn() -> C;
fn help(self, txt: C) -> Result<T>;
}Expand description
Trait that defines the behavior of errors that Sift manages.
This trait provides methods for adding context and help text to errors, allowing for rich error messages that guide users toward resolution.
§Example
use sift_error::prelude::*;
use std::io;
fn read_config() -> Result<String> {
std::fs::read_to_string("config.toml")
.map_err(|e| Error::new(ErrorKind::IoError, e))
.context("failed to read configuration file")
.help("ensure the config.toml file exists and is readable")
}Required Methods§
Sourcefn context(self, ctx: C) -> Result<T>
fn context(self, ctx: C) -> Result<T>
Adds context that is printed with the error.
Context is displayed as the most recent error message, with previous context forming a chain of causes.
§Example
use sift_error::prelude::*;
fn example() -> Result<()> {
let err = Error::new_msg(ErrorKind::IoError, "file not found");
Err(err).context("failed to load user data")
}Sourcefn with_context<F>(self, op: F) -> Result<T>where
F: Fn() -> C,
fn with_context<F>(self, op: F) -> Result<T>where
F: Fn() -> C,
Like context but takes in a closure.
This is useful when constructing the context string is expensive, as the closure is only called if there’s an error.
§Example
use sift_error::prelude::*;
fn example(user_id: &str) -> Result<()> {
let err = Error::new_msg(ErrorKind::NotFoundError, "resource missing");
Err(err).with_context(|| format!("user {} not found", user_id))
}Sourcefn help(self, txt: C) -> Result<T>
fn help(self, txt: C) -> Result<T>
User-help text.
Help text provides actionable guidance to users on how to resolve the error. It’s displayed separately from the error context.
§Example
use sift_error::prelude::*;
fn example() -> Result<()> {
let err = Error::new_msg(ErrorKind::ConfigError, "invalid config");
Err(err).help("check your sift.toml file for syntax errors")
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.