-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using eyre with Result<_,&str>
#16
Comments
I'll double check but im pretty sure it's not possible to impl for now I think the best approach to dealing with None.ok_or("test").map_err(|s| eyre!(s)) |
Thanks for the quick and clear answer @yaahc! |
My pleasure, I'm also going to add this to the docs so hopefully this is clear for others in the future /// # Wrapping Types That Don't impl `Error` (e.g. `&str` and `Box<dyn Error>`)
///
/// Due to restrictions for coherence `Report` cannot impl `From` for types that don't impl
/// `Error`. Attempts to do so will give "this type might implement Error in the future" as an
/// error. As such, `wrap_err`, which uses `From` under the hood, cannot be used to wrap these
/// types. Instead we encourage you to use the combinators provided for `Result` in `std`/`core`.
///
/// For example, instead of this:
///
/// ```rust,compile_fail
/// use std::error::Error;
/// use eyre::{WrapErr, Report};
///
/// fn wrap_example(err: Result<(), Box<dyn Error + Send + Sync + 'static>>) -> Result<(), Report> {
/// err.wrap_err("saw a downstream error")
/// }
/// ```
///
/// We encourage you to write this:
///
/// ```rust
/// use std::error::Error;
/// use eyre::{WrapErr, Report, eyre};
///
/// fn wrap_example(err: Result<(), Box<dyn Error + Send + Sync + 'static>>) -> Result<(), Report> {
/// err.map_err(|e| eyre!(e)).wrap_err("saw a downstream error")
/// }
/// ``` |
I am trying to use eyre for unwrapping
Option
s, for which I useok_or
and&str
as theErr
-type. I figured this would work with eyre, since&str
andString
implementstd::error::Error
. However, the following code does not compile:with
Do I need to implement
Report
for&str
to get this working? If so, maybe it's good to add theseReport
implementations for&str
andString
in the eyre crate itself?The text was updated successfully, but these errors were encountered: