Convert `Box` to `anyhow::Error`

⚓ rust    📅 2025-05-30    👤 surdeus    👁️ 2      

surdeus

The Server::http function in the tiny_http crate has the following return type:
Result<Server, Box<dyn Error + Send + Sync + 'static>>

Now I call that function, and my own function has return value anyhow::Result<...>. So I want to use the question mark operator ? to convert the error of the Server::http function into an anyhow::Error. But it complains:

error[E0277]: `?` couldn't convert the error: `dyn StdError + std::marker::Send + Sync: Sized` is not satisfied
   --> src/gui/oauth.rs:177:46
    |
175 | fn catch_redirect() -> anyhow::Result<RedirectArgs> {
    |                        ---------------------------- required `dyn StdError + std::marker::Send + Sync: Sized` because of this
176 |     // listen for request
177 |     let server = Server::http(SOCKET_ADDRESS)?;
    |                  ----------------------------^ doesn't have a size known at compile-time
    |                  |
    |                  this can't be annotated with `?` because it has type `Result<_, Box<(dyn StdError + std::marker::Send + Sync + 'static)>>`
    |
    = help: the trait `Sized` is not implemented for `dyn StdError + std::marker::Send + Sync`
    = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
    = note: required for `Box<dyn StdError + std::marker::Send + Sync>` to implement `StdError`
    = note: required for `anyhow::Error` to implement `From<Box<dyn StdError + std::marker::Send + Sync>>`

I also tried using .map_err(Into::into)? but it still complains:

error[E0282]: type annotations needed
   --> src/gui/oauth.rs:177:55
    |
177 |     let server = Server::http(SOCKET_ADDRESS).map_err(Into::into)?;
    |                                                       ^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the trait `Into`
    |
help: consider specifying the generic argument
    |
177 |     let server = Server::http(SOCKET_ADDRESS).map_err(Into::<T>::into)?;
    |                                                           +++++

error[E0283]: type annotations needed
   --> src/gui/oauth.rs:177:55
    |
177 |     let server = Server::http(SOCKET_ADDRESS).map_err(Into::into)?;
    |                                                       ^^^^^^^^^^ - type must be known at this point
    |                                                       |
    |                                                       cannot infer type of the type parameter `T` declared on the trait `Into`
    |
    = note: multiple `impl`s satisfying `anyhow::Error: From<_>` found in the following crates: `anyhow`, `core`:
            - impl<E> From<E> for anyhow::Error
              where E: StdError, E: std::marker::Send, E: Sync, E: 'static;
            - impl<T> From<T> for T;
help: consider specifying the generic argument
    |
177 |     let server = Server::http(SOCKET_ADDRESS).map_err(Into::<T>::into)?;
    |                                                           +++++

Same thing if I try to use .map_err(anyhow::Error::new):

error[E0277]: the size for values of type `dyn StdError + std::marker::Send + Sync` cannot be known at compilation time
   --> src/gui/oauth.rs:177:18
    |
177 |     let server = Server::http(SOCKET_ADDRESS).map_err(anyhow::Error::new)?;
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `dyn StdError + std::marker::Send + Sync`
    = help: the trait `StdError` is implemented for `Box<E>`
    = note: required for `Box<dyn StdError + std::marker::Send + Sync>` to implement `StdError`
note: required by a bound in `anyhow::error::<impl anyhow::Error>::new`
   --> /home/johann/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/anyhow-1.0.98/src/error.rs:36:12
    |
34  |     pub fn new<E>(error: E) -> Self
    |            --- required by a bound in this associated function
35  |     where
36  |         E: StdError + Send + Sync + 'static,
    |            ^^^^^^^^ required by this bound in `anyhow::error::<impl Error>::new`

error[E0277]: the size for values of type `dyn StdError + std::marker::Send + Sync` cannot be known at compilation time
   --> src/gui/oauth.rs:177:55
    |
177 |     let server = Server::http(SOCKET_ADDRESS).map_err(anyhow::Error::new)?;
    |                                                       ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `dyn StdError + std::marker::Send + Sync`
    = help: the trait `StdError` is implemented for `Box<E>`
    = note: required for `Box<dyn StdError + std::marker::Send + Sync>` to implement `StdError`
note: required by a bound in `anyhow::error::<impl anyhow::Error>::new`
   --> /home/johann/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/anyhow-1.0.98/src/error.rs:36:12
    |
34  |     pub fn new<E>(error: E) -> Self
    |            --- required by a bound in this associated function
35  |     where
36  |         E: StdError + Send + Sync + 'static,
    |            ^^^^^^^^ required by this bound in `anyhow::error::<impl Error>::new`

Any ideas?

3 posts - 2 participants

Read full topic

🏷️ rust_feed