-
Notifications
You must be signed in to change notification settings - Fork 138
Derive From? #10
Comments
@aturon and I agree that the pattern that's emerged so far of having something like #[derive(Fail)]
struct Error {
inner: Context<ErrorKind>,
}
#[derive(Fail)]
enum ErrorKind {
// the different kinds of "semantic" errors that could occur
} This enables a many-to-many relationship between the ErrorKinds and the underlying errors, so an io Error might mean different things in different contexts. What this means though is that you don't get If you don't want to plumb that extra context in around your error types, we would recommend not having this kind of wrapper at all and just using the If you disagree and really want something like an impl From<io::Error> for MyError {
fn from(err: io::Error) -> MyError { MyError::Io(err) }
} Since From is in the prelude and the types are already in scope, its only these 3 lines of code that you have to write. |
@withoutboats can you explain this a bit more? I have been looking for exactly this guidance as I worked on my own error library (which I think I shall abandon now, in favor of Specifically I'd like to better understand the many-to-many relationship that
|
Just for reference, here are a few (fairly randomly selected) codebases that all use the It'd be interesting to try removing the |
So having read your response @withoutboats a couple times and being a little puzzled by it I now I realized from impls shouldn’t be required due to how crate is designed (and the example has IO errors present in the body of the function but not explicitly wrapped by the error type/enum). This is a net win for me (and what I thought the crate was doing) as I dislike adding from lines and additional enum wrapping variants in either quicker error, error chain or custom errors. I have to investigate the cause (I probably just messed something up) but I was experimentally porting an old error from error chain to failure and it gave “no from impls” errors and would not work until I explicitly added them in the enum; but iiuc, this is should not be required at all using this crate (unless as you said, you really want to). So I think this issue can be closed since, iiuc, you should never have to wrap an error in order to use |
@m4b The example in the README uses the However, if you want to not use Does that clarify things? |
Yea it’s clear. Unfortunately this whole issue was result of a red herring (which i suspect other users will probably stumble into) The error I was converting (like many errors) was named Consequently one will see a bunch of errors about From not impld for each of the error variants present in the body (which was the impetus of this issue). (And will perhaps, like me, miss the warning that the Error from failure is an unused import) That’s why I was confused about your response because it sounded like you were saying no impl it yourself and I was like damn I dunno what value this crate adds if I have to do it all by hand again 😆 So yea this can be closed since what I was asking for is not necessary for the use I intended it for (error chain, custom hand rolled errors which wrap others in enum, etc, and auto derive from impl so it works with I hope it’s clear what i was confused about :P Maybe add a little gotcha or warning to not forget to not shadow the error type if your error is named Error? |
Putting my suggestion from IRC into a more permanent form: I think it would be great to have the ability to at least opt-in to |
@m4b I too was still in a mindset of thinking I manually needed to declare my |
@withoutboats i played with this quite a bit now and I think that |
@mitsuhiko I think the idea is to separate |
I disagree. This means using |
@Emm Not saying this feature won't ever be added, but the alternative is not |
@withoutboats, you are right, of course. I conflated two issues: writing |
Closing this. |
Sorry if this is addressed but I didn’t immediately see how
From<SomeError>
works for an error that wraps another error, e.g.io::Error
Are we currently expected to implement from conversion for error types wrapping other errors ourselves?
The text was updated successfully, but these errors were encountered: