Skip to content
Merged
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
26 changes: 25 additions & 1 deletion sentry-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,21 @@ pub fn event_from_error<E: Error + ?Sized>(err: &E) -> Event<'static> {

fn exception_from_error<E: Error + ?Sized>(err: &E) -> Exception {
let dbg = format!("{:?}", err);
let value = err.to_string();

// A generic `anyhow::msg` will just `Debug::fmt` the `String` that you feed
// it. Trying to parse the type name from that will result in a leading quote
// and the first word, so quite useless.
// To work around this, we check if the `Debug::fmt` of the complete error
// matches its `Display::fmt`, in which case there is no type to parse and
// we will just be using `Error`.
let ty = if dbg == format!("{:?}", value) {
String::from("Error")
} else {
parse_type_from_debug(&dbg).to_owned()
};
Exception {
ty: parse_type_from_debug(&dbg).to_owned(),
ty,
value: Some(err.to_string()),
..Default::default()
}
Expand Down Expand Up @@ -146,3 +159,14 @@ fn test_parse_type_from_debug() {
);
assert_eq!(parse(&err), "ParseIntError");
}

#[test]
fn test_parse_anyhow_as_error() {
let anyhow_err = anyhow::anyhow!("Ooops, something bad happened");
let err: &dyn Error = anyhow_err.as_ref();

let exc = exception_from_error(err);

assert_eq!(&exc.ty, "Error");
assert_eq!(exc.value.as_deref(), Some("Ooops, something bad happened"));
}