0

I'm new to rust, and facing an error when propagating different error types (from actix and sqlx in this case).

I saw that similar problems are addressed by changing the return of the function to the appropriate type, or to an enum of the different possible errors in these questions:

But in this case seems like I'm constrained to keep the same result because of the #[actix_web::main] macro, so that doesn't seem like an option.

What would be the fix in this case? I tried replacing -> std::io::Result<()> by -> anyhow::Result<()>, but I get a mismatched types error. I also implemented my own enum with thiserror, but same error happens.

My code is next:

use actix_web::{App, HttpServer};
use sqlx;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // The trailing `?` here is causing the error below
    let db_pool = sqlx::SqlitePool::connect("sqlite://mydb.db").await?;

    HttpServer::new(move || {
        App::new()
            .data(db_pool.clone())
    })
    .bind(("127.0.0.1", 7000))?
    .run()
    .await
}

This is the error I'm getting:

$ cargo run
   Compiling failing_example v0.1.0 (/home/mgarcia/src/isyourplan-backend/bug_minimal)
error[E0277]: `?` couldn't convert the error to `std::io::Error`
 --> src/main.rs:6:70
  |
6 |     let db_pool = sqlx::SqlitePool::connect("sqlite://mydb.db").await?;
  |                                                                      ^ the trait `From<sqlx::Error>` is not implemented for `std::io::Error`
  |
  = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
  = help: the following implementations were found:
            <std::io::Error as From<ErrorKind>>
            <std::io::Error as From<IntoInnerError<W>>>
            <std::io::Error as From<NulError>>
            <std::io::Error as From<brotli2::raw::Error>>
          and 12 others
  = note: required by `from`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `failing_example`

To learn more, run the command again with --verbose.

And in case it's useful, this is my Cargo.toml:

[package]
name = "failing_example"
version = "0.1.0"
authors = ["me"]
edition = "2018"

[dependencies]
actix-web = "3"
sqlx = { version = "0.5", features = ["runtime-actix-rustls", "sqlite"] }
Marc Garcia
  • 3,287
  • 2
  • 28
  • 37
  • tl;dr - Create your own error type which combines the two possible error types with a sum (enum). Several libraries exist to facilitate this, such as `thiserror` and `snafu`. – E_net4 Oct 26 '21 at 15:51
  • Thanks for the information, and for the linked question. I'm not able to fix my problem with the information you provided, I think my question is quite different from the one it's supposed to be a duplicate. I don't think closing as duplicate is correct, even if the answer to that question is part of the solution here. – Marc Garcia Oct 26 '21 at 19:13
  • 1
    https://meta.stackoverflow.com/questions/252252/this-question-already-has-answers-here-but-it-does-not-what-can-i-do-when-i – E_net4 Oct 26 '21 at 20:32
  • Regarding your edit, [this code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ddfb75fec81a57733a2fbfd389a0dd82) compiles for me. Simply changing the return type doesn't address that the error type of running the http server needs to be converted (via either `?` as I've done or by `.into()`) – kmdreko Oct 31 '21 at 01:37

0 Answers0