pub struct Context { /* private fields */ }Expand description
Builder describing how to convert an external error into AppError.
The context captures the target AppCode, AppErrorKind, optional
metadata fields and redaction policy. It is primarily consumed by
ResultExt when promoting Result<T, E> values into
AppError.
§Examples
use std::io::{Error as IoError, ErrorKind};
use masterror::{AppErrorKind, Context, ResultExt, field};
fn failing_io() -> Result<(), IoError> {
Err(IoError::from(ErrorKind::Other))
}
let err = failing_io()
.ctx(|| {
Context::new(AppErrorKind::Service)
.with(field::str("operation", "sync"))
.redact(true)
.track_caller()
})
.unwrap_err();
assert_eq!(err.kind, AppErrorKind::Service);
assert!(err.metadata().get("operation").is_some());Implementations§
Source§impl Context
impl Context
Sourcepub fn new(category: AppErrorKind) -> Self
pub fn new(category: AppErrorKind) -> Self
Create a new Context targeting the provided AppErrorKind.
The initial AppCode defaults to the canonical mapping for the
supplied kind. Use Context::code to override it.
Sourcepub fn category(self, category: AppErrorKind) -> Self
pub fn category(self, category: AppErrorKind) -> Self
Update the AppErrorKind.
When the code has not been overridden explicitly, it is kept in sync with the new kind.
§Examples
use masterror::{AppErrorKind, Context};
let ctx = Context::new(AppErrorKind::BadRequest).category(AppErrorKind::Service);Sourcepub fn redact_field(self, name: &'static str, redaction: FieldRedaction) -> Self
pub fn redact_field(self, name: &'static str, redaction: FieldRedaction) -> Self
Override the redaction policy for a metadata field.
§Examples
use masterror::{AppErrorKind, Context, FieldRedaction, field};
let ctx = Context::new(AppErrorKind::Service)
.with(field::str("password", "secret"))
.redact_field("password", FieldRedaction::Redact);Sourcepub fn redact_field_mut(
&mut self,
name: &'static str,
redaction: FieldRedaction,
) -> &mut Self
pub fn redact_field_mut( &mut self, name: &'static str, redaction: FieldRedaction, ) -> &mut Self
Override the redaction policy for a metadata field in place.
Sourcepub fn redact(self, redact: bool) -> Self
pub fn redact(self, redact: bool) -> Self
Toggle message redaction policy.
§Examples
use masterror::{AppErrorKind, Context};
let ctx = Context::new(AppErrorKind::Service).redact(true);Sourcepub fn track_caller(self) -> Self
pub fn track_caller(self) -> Self
Capture caller location and store it as metadata.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Context
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more