pub struct ErrorContext {
pub message: String,
pub source: Option<Box<dyn Error + Send + Sync + 'static>>,
pub metadata: Option<HashMap<String, String>>,
pub timestamp: String,
pub trace_id: String,
}
Expand description
A context wrapper for errors with additional metadata.
ErrorContext
provides a way to enrich errors with contextual information,
making them more useful for debugging and logging. Each error context includes:
- A descriptive message
- An optional source error
- Optional key-value metadata
- A timestamp (automatically generated)
- A unique trace ID (automatically generated)
This structure implements both Display
and std::error::Error
traits,
making it suitable for use in error handling chains.
Fields§
§message: String
The error message
source: Option<Box<dyn Error + Send + Sync + 'static>>
The source error that caused this error
metadata: Option<HashMap<String, String>>
Additional metadata about the error
timestamp: String
The timestamp of the error in RFC 3339 format
trace_id: String
The unique identifier for the error (UUID v4)
Implementations§
Source§impl ErrorContext
impl ErrorContext
Sourcepub fn new(
message: impl Into<String>,
source: Option<Box<dyn Error + Send + Sync + 'static>>,
metadata: Option<HashMap<String, String>>,
) -> Self
pub fn new( message: impl Into<String>, source: Option<Box<dyn Error + Send + Sync + 'static>>, metadata: Option<HashMap<String, String>>, ) -> Self
Creates a new error context with the given message, source, and metadata.
§Arguments
message
- A descriptive error messagesource
- An optional source error that caused this errormetadata
- Optional key-value pairs providing additional context
§Returns
A new ErrorContext
instance with automatically generated timestamp and trace ID.
Sourcepub fn new_with_log(
message: impl Into<String>,
source: Option<Box<dyn Error + Send + Sync + 'static>>,
metadata: Option<HashMap<String, String>>,
) -> Self
pub fn new_with_log( message: impl Into<String>, source: Option<Box<dyn Error + Send + Sync + 'static>>, metadata: Option<HashMap<String, String>>, ) -> Self
Creates a new error context and logs it with the given message, source, and metadata.
§Arguments
message
- A descriptive error messagesource
- An optional source error that caused this errormetadata
- Optional key-value pairs providing additional context
§Returns
A new ErrorContext
instance with automatically generated timestamp and trace ID.
Sourcepub fn format_with_metadata(&self) -> String
pub fn format_with_metadata(&self) -> String
Formats the error message with its metadata appended in a readable format.
The format is: "message [key1=value1, key2=value2, ...]"
.
Metadata keys are sorted alphabetically for consistent output.
§Returns
A formatted string containing the error message and its metadata.
Trait Implementations§
Source§impl Debug for ErrorContext
impl Debug for ErrorContext
Source§impl Display for ErrorContext
impl Display for ErrorContext
Source§impl Error for ErrorContext
impl Error for ErrorContext
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
impl Send for ErrorContext
impl Sync for ErrorContext
Auto Trait Implementations§
impl Freeze for ErrorContext
impl !RefUnwindSafe for ErrorContext
impl Unpin for ErrorContext
impl !UnwindSafe for ErrorContext
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
§impl<T> Chain<T> for T
impl<T> Chain<T> for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more