Skip to main content

StackError

Trait StackError 

Source
pub trait StackError: Error {
    // Required methods
    fn location(&self) -> Location;
    fn type_name(&self) -> &'static str;

    // Provided methods
    fn stack_source(&self) -> Option<&dyn StackError> { ... }
    fn depth(&self) -> usize { ... }
}
Expand description

Error trait extension that adds source code location tracking.

Types implementing this trait carry a Location at each level of the error chain, enabling StackReport to produce stack-trace-like output.

§Design note: no Send + Sync supertrait

This trait requires only Error, not Send + Sync + 'static (unlike anyhow/eyre). This aligns with snafu, which does not impose Send + Sync on error types. BoxedStackError adds Send + Sync bounds for the thread-safe trait object case.

§Design note: unsealed trait

This trait is intentionally unsealed — external crates may implement it for custom wrapper types (e.g., similar to BoxedStackError). Future method additions must provide default implementations to avoid breaking downstream impls.

§Deriving

Use #[suzunari_error] (recommended) or #[derive(StackError)] directly. Both resolve the location field via #[stack(location)] or by detecting a Location-typed field. Manual impl is only needed for wrapper types like BoxedStackError.

§Example

use suzunari_error::*;

#[suzunari_error]
#[suzu(display("fetch failed for {url}"))]
struct FetchError {
    url: String,
    source: std::io::Error,
}

fn fetch(url: &str) -> Result<(), FetchError> {
    std::fs::read(url).context(FetchSnafu { url })?;
    Ok(())
}

let err = fetch("/nonexistent").unwrap_err();

// StackError methods:
assert!(err.location().file().ends_with(".rs"));
assert_eq!(err.type_name(), "FetchError");
assert!(err.stack_source().is_none()); // io::Error is not StackError
assert_eq!(err.depth(), 1);            // 1 cause in the chain

Required Methods§

Source

fn location(&self) -> Location

Returns the location where this error was constructed.

Source

fn type_name(&self) -> &'static str

Returns a human-readable type name for display in stack traces.

The derive macro generates this as a &'static str literal:

  • Structs: "StructName"
  • Enum variants: "EnumName::VariantName"

Generic type parameters are not included. This is intended for display purposes only — do not parse or match against it programmatically.

Provided Methods§

Source

fn stack_source(&self) -> Option<&dyn StackError>

Returns the source error as a StackError, if available.

This enables StackReport to traverse the error chain with location info. The derive macro generates this automatically using autoref specialization (see the __private module).

§Contract

If stack_source() returns Some(s), then Error::source() must also return Some(e) where e and s refer to the same underlying error value (i.e., s is a &dyn StackError view of the &dyn Error returned by source()). The derive macro upholds this automatically; manual impls must ensure consistency.

Violating this contract causes StackReport to produce incomplete output in release builds (the debug_assert! that checks this is stripped). In debug builds, a panic will occur instead.

Source

fn depth(&self) -> usize

Returns the number of errors in the Error::source() chain (excluding self).

Traverses the full Error::source() chain (not stack_source()), counting both StackError and non-StackError causes.

Note: this count may differ from the number of lines in StackReport output, which also shows the top-level error on the first line.

Trait Implementations§

Source§

impl Error for Box<dyn StackError>

Routes Error::source through the trait object.

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl Error for Box<dyn StackError + Send + Sync>

Routes Error::source through the thread-safe trait object.

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<BoxedStackError> for Box<dyn StackError + Send + Sync>

Source§

fn from(inner: BoxedStackError) -> Self

Converts to this type from the input type.
Source§

impl StackError for Box<dyn StackError>

Delegates all methods through the dyn StackError trait object.

Source§

fn location(&self) -> Location

Returns the location where this error was constructed.
Source§

fn type_name(&self) -> &'static str

Returns a human-readable type name for display in stack traces. Read more
Source§

fn stack_source(&self) -> Option<&dyn StackError>

Returns the source error as a StackError, if available. Read more
Source§

fn depth(&self) -> usize

Returns the number of errors in the Error::source() chain (excluding self). Read more
Source§

impl StackError for Box<dyn StackError + Send + Sync>

Delegates all methods through the dyn StackError + Send + Sync trait object.

Source§

fn location(&self) -> Location

Returns the location where this error was constructed.
Source§

fn type_name(&self) -> &'static str

Returns a human-readable type name for display in stack traces. Read more
Source§

fn stack_source(&self) -> Option<&dyn StackError>

Returns the source error as a StackError, if available. Read more
Source§

fn depth(&self) -> usize

Returns the number of errors in the Error::source() chain (excluding self). Read more

Implementations on Foreign Types§

Source§

impl StackError for Box<dyn StackError + Send + Sync>

Delegates all methods through the dyn StackError + Send + Sync trait object.

Source§

fn location(&self) -> Location

Source§

fn type_name(&self) -> &'static str

Source§

fn stack_source(&self) -> Option<&dyn StackError>

Source§

impl StackError for Box<dyn StackError>

Delegates all methods through the dyn StackError trait object.

Source§

fn location(&self) -> Location

Source§

fn type_name(&self) -> &'static str

Source§

fn stack_source(&self) -> Option<&dyn StackError>

Source§

impl<T: StackError> StackError for Box<T>

Delegates all methods to the inner T.

Requires T: Sized; Box<dyn StackError> needs a separate impl because core provides impl Error for Box<T: Error + ?Sized> but we still need to manually route StackError methods.

Source§

fn location(&self) -> Location

Source§

fn type_name(&self) -> &'static str

Source§

fn stack_source(&self) -> Option<&dyn StackError>

Source§

impl<T: ?Sized + StackError> StackError for Arc<T>

Delegates all methods to the inner T via Arc::as_ref.

Source§

fn location(&self) -> Location

Source§

fn type_name(&self) -> &'static str

Source§

fn stack_source(&self) -> Option<&dyn StackError>

Implementors§