OhnoCore

Struct OhnoCore 

Source
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

Source

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();
Source

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);
Source

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());
Source

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());
Source

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).

Source

pub fn context_iter(&self) -> impl Iterator<Item = &TraceInfo>

Returns an iterator over the context information in reverse order (most recent first).

Source

pub fn context_messages(&self) -> impl Iterator<Item = &str>

Returns an iterator over just the context messages in reverse order (most recent first).

Source

pub fn format_message( &self, default_message: &str, override_message: Option<Cow<'_, str>>, ) -> String

Formats the main error message without backtrace or error traces.

Source

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.

Trait Implementations§

Source§

impl Debug for OhnoCore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for OhnoCore

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for OhnoCore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl ErrorTrace for OhnoCore

Source§

fn add_error_trace(&mut self, trace: TraceInfo)

Adds error trace information to the error. Read more
Source§

impl<T> From<T> for OhnoCore
where T: Into<Box<dyn StdError + Send + Sync>>,

Source§

fn from(value: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErrorTraceExt for T
where T: ErrorTrace,

Source§

fn error_trace(self, trace: impl Into<Cow<'static, str>>) -> Self
where Self: Sized,

Wraps the error with error trace.
Source§

fn detailed_error_trace( self, trace: impl Into<Cow<'static, str>>, file: &'static str, line: u32, ) -> Self
where Self: Sized,

Wraps the error with detailed error trace including file and line information.
Source§

fn with_error_trace<F, R>(self, f: F) -> Self
where F: FnOnce() -> R, R: Into<Cow<'static, str>>, Self: Sized,

Wraps the error with lazily evaluated error trace.
Source§

fn with_detailed_error_trace<F, R>( self, f: F, file: &'static str, line: u32, ) -> Self
where F: FnOnce() -> R, R: Into<Cow<'static, str>>, Self: Sized,

Wraps the error with lazily evaluated detailed error trace including file and line information.
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.