Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ btparse = "0.2.0"
color-backtrace = { version = "0.7.0", features = ["use-btparse-crate"] }
snafu = { version = "0.8.5", features = ["rust_1_81", "std", "backtraces-impl-backtrace-crate"] }
tracing-error = "0.2.1"

[dev-dependencies]
nested_enum_utils = "0.2.2"
98 changes: 98 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,101 @@ pub use self::{
error::{Error, Result, ResultExt},
spantrace::SpanTrace,
};

#[cfg(test)]
mod tests {
use super::*;
use nested_enum_utils::common_fields;
use snafu::{Backtrace, ErrorCompat, ResultExt, Snafu};

#[common_fields({
backtrace: Option<Backtrace>,
#[snafu(implicit)]
span_trace: SpanTrace,
})]
#[allow(missing_docs)]
#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum EnumNoTransparent {
#[snafu(display("This error wraps another enum"))]
WrapAnotherEnum { source: AnotherEnumError },
#[snafu(display("This error wraps a struct"))]
WrapAStruct { source: StructError },
}

#[derive(Debug, Snafu)]
#[snafu(display("This is a struct error"))]
pub struct StructError {
backtrace: Option<Backtrace>,
}

#[common_fields({
backtrace: Option<Backtrace>,
#[snafu(implicit)]
span_trace: SpanTrace,
})]
#[allow(missing_docs)]
#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum EnumTransparent {
#[snafu(transparent)]
#[allow(dead_code)]
TransparentStruct { source: StructError },
}

#[common_fields({
backtrace: Option<Backtrace>,
#[snafu(implicit)]
span_trace: SpanTrace,
})]
#[allow(missing_docs)]
#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum AnotherEnumError {
#[snafu(display("This is a variant error in another enum"))]
ErrorInAnotherEnum {},
}

fn enum_no_transparent_another_enum() -> std::result::Result<(), EnumNoTransparent> {
another_enum().context(WrapAnotherEnumSnafu)?;
Ok(())
}

fn enum_no_transparent_struct() -> std::result::Result<(), EnumNoTransparent> {
struct_error().context(WrapAStructSnafu)?;
Ok(())
}

fn enum_transparent_struct() -> std::result::Result<(), EnumTransparent> {
struct_error()?;
Ok(())
}

fn struct_error() -> std::result::Result<(), StructError> {
let err = StructSnafu {}.build();
println!("STRUCT BACKTRACE{:?}", err.backtrace());
Err(err)
}

fn another_enum() -> std::result::Result<(), AnotherEnumError> {
Err(ErrorInAnotherEnumSnafu {}.build())
}

#[test]
fn test_another_enum() -> Result<()> {
enum_no_transparent_another_enum()?;
Ok(())
}

#[test]
fn test_wrap_a_struct() -> Result<()> {
enum_no_transparent_struct()?;
Ok(())
}

#[test]
fn test_transparent() -> Result<()> {
enum_transparent_struct()?;
Ok(())
}
}
Loading