rustrust-tokiotauritauri-2

async tauri command returning dynamic error, does not implement necessary traits


im trying to give a client application email functionality using lettre and tokio.

The code itself should work, though declaring the function a tauri::command gives problems, i don't know how to solve.

#[tauri::command]
async fn send_email(
    from: &str,    
    to: &str,    
    subject: &str,   
    body: String,) 
->Result<(), Box<dyn std::error::Error>> {
    let smtp_credentials: Credentials =
        Credentials::new("smtp_username".to_string(), "smtp_password".to_string());

    let mailer = AsyncSmtpTransport::<Tokio1Executor>::relay("smtp.email.com")?
        .credentials(smtp_credentials)
        .build();

    let email = Message::builder()
        .from(from.parse()?)
        .to(to.parse()?)
        .subject(subject)
        .body(body.to_string())?;

    mailer.send(email).await?;

    Ok(())
}

throwing me the error:

the method `async_kind` exists for reference `&impl Future<Output = Result<(), Box<dyn Error>>>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`impl Future<Output = Result<(), Box<(dyn std::error::Error + 'static)>>>: IpcResponse`
which is required by `&impl Future<Output = Result<(), Box<(dyn std::error::Error + 'static)>>>: tauri::ipc::private::ResponseKind`
`<&impl Future<Output = Result<(), Box<(dyn std::error::Error + 'static)>>> as Future>::Output = Result<_, _>`
which is required by `&impl Future<Output = Result<(), Box<(dyn std::error::Error + 'static)>>>: tauri::ipc::private::ResultFutureKind`
`&impl Future<Output = Result<(), Box<(dyn std::error::Error + 'static)>>>: Future`
which is required by `&impl Future<Output = Result<(), Box<(dyn std::error::Error + 'static)>>>: tauri::ipc::private::ResultFutureKind`


i have tried impl the Future trait for the return type of my function like:

send_mail (...) ->impl Future <Output = Result<(), Box<dyn std::error::Error>>> {

to no success

the problems seems to be the return value of a dynamic error because the standard library error does not impl Future but trying to implement the trait for the dyn Error or Box of typ dyn Error is not possible.

I know i could refactor my code so the tauri command invokes my async function,letting it handle the dyn Error by propegation so eliminating to use both in one but i would at least like to know why this error is caused/how to fix it properly.

Im fairly new to rust/tauri, and while i like to think to understand the concepts and restriction but im still fighting with the expected syntax.

thanks in advance for the help


Solution

  • In short, the error type needs to implement Serialize which Box<dyn Error> does not.

    The Tauri docs have a section on error-handling. From there:

    There is more there than just these examples; I encourage you to read through it.