Catch panic in async call

⚓ Rust    📅 2025-10-09    👤 surdeus    👁️ 4      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Catch panic in async call

Hi,

I would like to catch the possible panic when calling a async function.
I read this documentation : JoinHandle in tokio::task - Rust
and I intend to use this kind of code :

let panics = tokio::spawn(async move {
                        test.run().await
                    });
                    
match panics {
// do something according if it is ok or err
...

The run function looks like this :

pub async fn run(&self) -> Result<()> {
        ...
}

With result like :

pub type Result<T> = core::result::Result<T, Error>;
pub type Error = Box<dyn std::error::Error>;

Actually I got this error :

let panics = tokio::spawn(async move {
    |  __________________________________------------_^
    | |                                  |
    | |                                  required by a bound introduced by this call
91  | |                         test.run().await
92  | |                     });
    | |_____________________^ `dyn StdError` cannot be sent between threads safely
    |
    = help: the trait `Send` is not implemented for `dyn StdError`
    = note: required for `Unique<dyn StdError>` to implement `Send`

I understant that Send must be add somewhere, but I don't know if it is in error type or else ?
If I add like this, I have a lot more error everywhere... :

pub type Error = Box<dyn std::error::Error + Send>;

9 posts - 4 participants

Read full topic

🏷️ Rust_feed