Recently I tried to test how my code behaves on occurrence of a panic. With only libstd facilities, it turned out to be fraught with problems: panics caused on purpose are cumbersome to distinguish from bona fide errors and assertion failures, and the default panic hook treats any panic as worth reporting it on stderr and even dumping the stack trace depending on the environment. To top it all, unwrap
on a thread::Result
cannot look into the panic message to include it in the panic message of the calling thread.
I've found no easy-to-use crate that would provide robust solutions to the above, so I decided to write my own: panic-control. It provides the following features:
-
A generic
Context
type, parameterized with a user-provided type that designates expected panics. Itsspawn()
method returns a wrapper overJoinHandle
that can inspect the panic payload returned byJoinHandle::join()
and if it matches the originatingContext
's parameter type, return it in the statically typedOk
variant signifying an expected panic. -
Extension trait
ThreadResultExt
providesstd::thread::Result
with a method to inspect the string payload of theErr
, should one be found, and a variant ofunwrap
that can propagate the panic message of the child thread. -
Facilities to augment the panic hook with filters suppressing its invocation when the panic occurs in a given thread, when the panic payload downcasts to a particular type, or determined by a free-form binary predicate closure called with the hook's parameter.
If you have similar issues with testing panics or overriding the panic hook, try this crate for a solution. Feel free to report if the utilities could be improved in any way.