-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
std::backtrace::Backtrace
`as the default backtrace provider
Closes #357
- Loading branch information
1 parent
9bfbf49
commit cf29c73
Showing
15 changed files
with
172 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use snafu::{prelude::*, Backtrace, ErrorCompat}; | ||
|
||
type AnotherError = Box<dyn std::error::Error>; | ||
|
||
#[derive(Debug, Snafu)] | ||
enum Error { | ||
#[snafu(display("Invalid user {}:\n{}", user_id, backtrace))] | ||
InvalidUser { user_id: i32, backtrace: Backtrace }, | ||
WithSource { | ||
source: AnotherError, | ||
backtrace: Backtrace, | ||
}, | ||
WithSourceAndOtherInfo { | ||
user_id: i32, | ||
source: AnotherError, | ||
backtrace: Backtrace, | ||
}, | ||
} | ||
|
||
type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
||
fn example(user_id: i32) -> Result<()> { | ||
ensure!(user_id >= 42, InvalidUserSnafu { user_id }); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn display_can_access_backtrace() { | ||
let e = example(0).unwrap_err(); | ||
let text = e.to_string(); | ||
assert!( | ||
text.contains("disabled backtrace"), | ||
"{:?} does not contain expected text", | ||
text | ||
); | ||
} | ||
|
||
fn trigger() -> Result<(), AnotherError> { | ||
Err("boom".into()) | ||
} | ||
|
||
#[test] | ||
fn errors_with_sources_can_have_backtraces() { | ||
let e = trigger().context(WithSourceSnafu).unwrap_err(); | ||
let backtrace = ErrorCompat::backtrace(&e).unwrap(); | ||
assert!(backtrace.to_string().contains("disabled backtrace")); | ||
} | ||
|
||
#[test] | ||
fn errors_with_sources_and_other_info_can_have_backtraces() { | ||
let e = trigger() | ||
.context(WithSourceAndOtherInfoSnafu { user_id: 42 }) | ||
.unwrap_err(); | ||
let backtrace = ErrorCompat::backtrace(&e).unwrap(); | ||
assert!(backtrace.to_string().contains("disabled backtrace")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,3 +121,5 @@ mod report { | |
let _: Result<(), snafu::Report<Error>> = mainlike_result(); | ||
} | ||
} | ||
|
||
mod backtraces; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pub use backtrace::Backtrace; | ||
|
||
impl crate::GenerateImplicitData for Backtrace { | ||
fn generate() -> Self { | ||
Backtrace::new() | ||
} | ||
} | ||
|
||
impl crate::AsBacktrace for Backtrace { | ||
fn as_backtrace(&self) -> Option<&Backtrace> { | ||
Some(self) | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pub use std::backtrace::Backtrace; | ||
|
||
impl crate::GenerateImplicitData for Backtrace { | ||
fn generate() -> Self { | ||
Backtrace::force_capture() | ||
} | ||
} | ||
|
||
impl crate::AsBacktrace for Backtrace { | ||
fn as_backtrace(&self) -> Option<&Backtrace> { | ||
Some(self) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.