Skip to content
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

Auto-context for simple pass-through errors (like thiserror) #276

Closed
hgomersall opened this issue Jan 19, 2021 · 6 comments
Closed

Auto-context for simple pass-through errors (like thiserror) #276

hgomersall opened this issue Jan 19, 2021 · 6 comments
Labels
support request Help requested on using the project

Comments

@hgomersall
Copy link

hgomersall commented Jan 19, 2021

I've found that thiserror and snafu fill two distinct roles for me. In the case when I have complex errors that take extra arguments, SNAFU is fantastic. However, in the case when module errors are simple wrappers around library errors, thiserror is simpler to use and syntactically tighter. It's also the case that one often gets name collisions with the intermediate structure and the library error, so this doesn't work (which is often frustrating):

use a_lib::{library_function, LibraryError};

#[derive(Debug, Snafu)]
pub enum MyError {
    #[snafu(display("Error : {}", source))]
    LibraryError { source: LibraryError },
}
fn do_something -> Result<(), MyError>
{
    library_function.context(LibraryError)?;
    Ok(())
}

It occurred to me that the SNAFU could fill both roles if in the simple case, the context could be inferred, much like with thiserror. Obviously, this depends on being able to disambiguate it, but that would still include many situations.

The solution might even be to include thiserror like syntax for pass-through errors:

use a_lib::{library_function, LibraryError};
use std::io::{self, Write};

#[derive(Debug, Snafu)]
pub enum MyError {
    #[snafu(display("Error : {}", source))]
    LibraryError { #[from] LibraryError },
    #[snafu(display("Error number: {}, source: {}", number, source))]
    AMoreComplicatedError { source: io::Error, number: usize },
}
fn do_something() -> Result<(), MyError>
{
    library_function?;
    io::stdout().write_all(b"hello world").context( AMoreComplicatedError { number: 10 })?;
    Ok(())
}
@hgomersall
Copy link
Author

There's likely a few errors in that code, but I think the point is apparent.

@shepmaster
Copy link
Owner

You can do that in SNAFU today:

#[derive(Debug, Snafu)]
pub enum MyError {
    #[snafu(display("Error : {}", source))]
    #[snafu(context(false))] // <--- Here
    LibraryError { source: LibraryError },

    #[snafu(display("Error number: {}, source: {}", number, source))]
    AMoreComplicatedError { source: io::Error, number: usize },
}

#199 tracks making this work as LibraryError(LibraryError)

@shepmaster shepmaster added the support request Help requested on using the project label Jan 19, 2021
@hgomersall
Copy link
Author

My bad. So LibraryError(LibraryError) is currently not supported?

@hgomersall
Copy link
Author

Also, does it still create the hidden context structs?

Thanks!

@shepmaster
Copy link
Owner

So LibraryError(LibraryError) is currently not supported?

That is correct.

does it still create the hidden context structs?

It does not.

@hgomersall
Copy link
Author

Great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support request Help requested on using the project
Projects
None yet
Development

No branches or pull requests

2 participants