pub struct OhnoCore { /* private fields */ }Expand description
Core error type that wraps source errors, captures backtraces, and holds context messages.
OhnoCore is the foundation of the ohno error handling system. It can wrap any error
type while providing automatic backtrace capture and context stacking capabilities.
The internal error data is boxed to keep the Err variant in Result small. This minimizes
cases where the Err is larger than the Ok variant. If the error only contains a
OhnoCore field, the size of Err will be equivalent to that of a raw pointer.
§Examples
use std::io;
use ohno::{ErrorTraceExt, OhnoCore};
// Create from a string message
let error = OhnoCore::from("something went wrong")
.error_trace("while processing request")
.error_trace("in user handler");
// Wrap an existing error
let io_error = io::Error::new(io::ErrorKind::NotFound, "file.txt");
let wrapped = OhnoCore::from(io_error).error_trace("failed to load config");Implementations§
Source§impl OhnoCore
impl OhnoCore
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new OhnoCore with no source (useful when using display override).
Automatically captures a backtrace at the point of creation.
§Examples
let error = ohno::OhnoCore::new();Sourcepub fn without_backtrace(
error: impl Into<Box<dyn StdError + Send + Sync + 'static>>,
) -> Self
pub fn without_backtrace( error: impl Into<Box<dyn StdError + Send + Sync + 'static>>, ) -> Self
Creates a new OhnoCore wrapping an existing error.
The wrapped error becomes the source in the error chain. Backtrace capture is disabled assuming the source error already has one.
§Examples
use std::io;
use ohno::OhnoCore;
let io_error = io::Error::new(io::ErrorKind::PermissionDenied, "access denied");
let wrapped = OhnoCore::without_backtrace(io_error);Sourcepub fn source(&self) -> Option<&(dyn StdError + 'static)>
pub fn source(&self) -> Option<&(dyn StdError + 'static)>
Returns the source error if this error wraps another error.
§Examples
use std::io;
use ohno::OhnoCore;
let io_error = io::Error::new(io::ErrorKind::NotFound, "file.txt");
let wrapped = OhnoCore::from(io_error);
assert!(wrapped.source().is_some());Sourcepub fn has_backtrace(&self) -> bool
pub fn has_backtrace(&self) -> bool
Returns whether this error has a captured backtrace.
§Examples
use ohno::OhnoCore;
let error = OhnoCore::from("test error");
// Backtrace capture depends on RUST_BACKTRACE environment variable
println!("Has backtrace: {}", error.has_backtrace());Sourcepub fn backtrace(&self) -> &Backtrace
pub fn backtrace(&self) -> &Backtrace
Returns a reference to the backtrace regardless of capture status.
This method always returns a reference to the internal backtrace, even if it wasn’t captured (in which case it will be empty/disabled).
Sourcepub fn context_iter(&self) -> impl Iterator<Item = &TraceInfo>
pub fn context_iter(&self) -> impl Iterator<Item = &TraceInfo>
Returns an iterator over the context information in reverse order (most recent first).
Sourcepub fn context_messages(&self) -> impl Iterator<Item = &str>
pub fn context_messages(&self) -> impl Iterator<Item = &str>
Returns an iterator over just the context messages in reverse order (most recent first).
Sourcepub fn format_message(
&self,
default_message: &str,
override_message: Option<Cow<'_, str>>,
) -> String
pub fn format_message( &self, default_message: &str, override_message: Option<Cow<'_, str>>, ) -> String
Formats the main error message without backtrace or error traces.
Sourcepub fn format_error(
&self,
f: &mut Formatter<'_>,
default_message: &str,
override_message: Option<Cow<'_, str>>,
) -> Result
pub fn format_error( &self, f: &mut Formatter<'_>, default_message: &str, override_message: Option<Cow<'_, str>>, ) -> Result
Formats the error with an optional custom message override.
This method is used internally by the Display implementation and by derived Error types that want to override the main error message.
§Errors
This function returns a fmt::Error if writing to the formatter fails.