@@ -98,8 +98,21 @@ pub fn event_from_error<E: Error + ?Sized>(err: &E) -> Event<'static> {
9898
9999fn exception_from_error < E : Error + ?Sized > ( err : & E ) -> Exception {
100100 let dbg = format ! ( "{:?}" , err) ;
101+ let value = err. to_string ( ) ;
102+
103+ // A generic `anyhow::msg` will just `Debug::fmt` the `String` that you feed
104+ // it. Trying to parse the type name from that will result in a leading quote
105+ // and the first word, so quite useless.
106+ // To work around this, we check if the `Debug::fmt` of the complete error
107+ // matches its `Display::fmt`, in which case there is no type to parse and
108+ // we will just be using `Error`.
109+ let ty = if dbg == format ! ( "{:?}" , value) {
110+ String :: from ( "Error" )
111+ } else {
112+ parse_type_from_debug ( & dbg) . to_owned ( )
113+ } ;
101114 Exception {
102- ty : parse_type_from_debug ( & dbg ) . to_owned ( ) ,
115+ ty,
103116 value : Some ( err. to_string ( ) ) ,
104117 ..Default :: default ( )
105118 }
@@ -146,3 +159,14 @@ fn test_parse_type_from_debug() {
146159 ) ;
147160 assert_eq ! ( parse( & err) , "ParseIntError" ) ;
148161}
162+
163+ #[ test]
164+ fn test_parse_anyhow_as_error ( ) {
165+ let anyhow_err = anyhow:: anyhow!( "Ooops, something bad happened" ) ;
166+ let err: & dyn Error = anyhow_err. as_ref ( ) ;
167+
168+ let exc = exception_from_error ( err) ;
169+
170+ assert_eq ! ( & exc. ty, "Error" ) ;
171+ assert_eq ! ( exc. value. as_deref( ) , Some ( "Ooops, something bad happened" ) ) ;
172+ }
0 commit comments