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:
- `?` couldn't convert the error to `std::io::Error`
- What is the correct error part of the Result type of a function propagating different error types?
- Rust proper error handling (auto convert from one error type to another with question mark)
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"] }