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

Implement std::error::Error for errors #27

Open
runfalk opened this issue Jan 21, 2020 · 3 comments
Open

Implement std::error::Error for errors #27

runfalk opened this issue Jan 21, 2020 · 3 comments

Comments

@runfalk
Copy link
Contributor

runfalk commented Jan 21, 2020

Currently the error type is Err(String) for most (maybe all) operations. This means you can't use this library with something like anyhow.

I've heard good things about thiserror. Maybe that's an alternative?

@pedrocr
Copy link
Owner

pedrocr commented Jan 21, 2020

There's already an error type that uses std::error::Error:

rawloader/src/lib.rs

Lines 73 to 98 in 9c13d51

/// Error type for any reason for the decode to fail
#[derive(Debug)]
pub struct RawLoaderError {
msg: String,
}
impl fmt::Display for RawLoaderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "RawLoaderError: \"{}\"", self.msg)
}
}
impl Error for RawLoaderError {
// Implement description so that older versions of rust still work
fn description(&self) -> &str {
"description() is deprecated; use Display"
}
}
impl RawLoaderError {
fn new(msg: String) -> Self {
Self {
msg,
}
}
}

What else would be needed?

@runfalk
Copy link
Contributor Author

runfalk commented Jan 21, 2020

I didn't realise there was such a struct already in place. It should be enough for anyhow. It just requires std::error::Error + Send + Sync + 'static.

I can try to update all places that still use a String and make them use RawLoaderError instead then?

@pedrocr
Copy link
Owner

pedrocr commented Feb 1, 2020

Go ahead if you want. The reason I didn't bother was that those are all internal to the crate and so don't affect the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants