When working with synchronous code, I can use panic::catch_unwind
like this:
#[actix_rt::test]
async fn test_sync() -> Result<(), Error> {
println!("before catch_unwind");
let sync_result = panic::catch_unwind(|| {
println!("inside sync catch_unwind");
panic!("this is error")
});
println!("after catch_unwind");
assert!(sync_result.is_ok());
Ok(())
}
How do I do the same when working with async code that is executed inside the catch_unwind
block? I can't figure out how to run the block while also being able to run some code after the block and finally assert the result.
This is what I have so far:
#[actix_rt::test]
async fn test_async() -> Result<(), Error> {
println!("before catch_unwind");
let async_result = panic::catch_unwind(|| async {
println!("inside async catch_unwind");
panic!("this is error")
}).await;
println!("after catch_unwind");
assert!(async_result.is_ok());
Ok(())
}