@@ -53,6 +53,15 @@ impl Backtrace {
5353 self . frames . clear ( ) ;
5454 }
5555
56+ /// Sets the cause of a backtrace.
57+ ///
58+ /// This is useful to stamp a backtrace with its cause after the frames
59+ /// have been collected, such as when we ultimately handle a fatal error at
60+ /// the top of its propagation chain.
61+ pub fn set_cause ( & mut self , cause : Cause ) {
62+ self . cause = Some ( cause) ;
63+ }
64+
5665 /// Push a "frame" (actor exit) onto the backtrace.
5766 ///
5867 /// This should be called every time an actor exits.
@@ -89,7 +98,8 @@ impl Display for Frame {
8998 }
9099}
91100
92- struct SyscallCause {
101+ #[ derive( Clone , Debug ) ]
102+ pub struct SyscallCause {
93103 /// The syscall "module".
94104 pub module : & ' static str ,
95105 /// The syscall function name.
@@ -100,13 +110,24 @@ struct SyscallCause {
100110 pub message : String ,
101111}
102112
113+ #[ derive( Clone , Debug ) ]
114+ pub struct FatalCause {
115+ /// The error message from the error.
116+ pub error_msg : String ,
117+ /// The original cause that initiated the error chain.
118+ pub root_cause : String ,
119+ /// The backtrace, captured if the relevant
120+ /// [environment variables](https://doc.rust-lang.org/std/backtrace/index.html#environment-variables) are enabled.
121+ pub backtrace : String ,
122+ }
123+
103124/// The ultimate "cause" of a failed message.
104125#[ derive( Clone , Debug ) ]
105126pub enum Cause {
106127 /// The original cause was a syscall error.
107128 Syscall ( SyscallCause ) ,
108129 /// The original cause was a fatal error.
109- Fatal ( String ) ,
130+ Fatal ( FatalCause ) ,
110131}
111132
112133impl Cause {
@@ -122,7 +143,11 @@ impl Cause {
122143
123144 /// Records a fatal error as the cause of a backtrace.
124145 pub fn from_fatal ( err : anyhow:: Error ) -> Self {
125- Self :: Fatal ( err. into ( ) )
146+ Self :: Fatal ( FatalCause {
147+ error_msg : err. to_string ( ) ,
148+ root_cause : err. root_cause ( ) . to_string ( ) ,
149+ backtrace : err. backtrace ( ) . to_string ( ) ,
150+ } )
126151 }
127152}
128153
@@ -137,7 +162,11 @@ impl Display for Cause {
137162 )
138163 }
139164 Cause :: Fatal ( msg) => {
140- write ! ( f, "[FATAL] {}" , msg)
165+ write ! (
166+ f,
167+ "[FATAL] Root cause: {}, Stacktrace: {}" ,
168+ msg. root_cause, msg. backtrace
169+ )
141170 }
142171 }
143172 }
0 commit comments