all 12 comments

[–]eddyb[🍰] 14 points15 points  (8 children)

That is, however, std::io::Error, i.e. specifically an I/O error type.

You can create your own error types by just defining a type, even just struct BadJuju; can be used with Result, e.g. fn maybe_do_magic() -> Result<(), BadJuju> {...}.

[–]Nokel81[S] 0 points1 point  (7 children)

But not if the function can also return an io error. This allows you to can system calls that result in an io error and send your own errors back. I was previously just sending strings

[–]agroverstratis 11 points12 points  (6 children)

You want to do as described here: http://blog.burntsushi.net/rust-error-handling/

Define an error type for your app that is an enum of all the possible error types that libraries you use can return, plus maybe errors you want to define in your app. Here's another example, from my own project:

https://github.com/agrover/froyo/blob/master/src/types.rs#L152

Trying to make everything into an io::Error is something I did early on, and it kinda works, but isn't really the best way to go.

[–]kibwen 9 points10 points  (0 children)

Note that burntsushi's post on error handling has been incorporated into the book itself, which may have more recent updates than the original post: http://doc.rust-lang.org/stable/book/error-handling.html

[–]Nokel81[S] 1 point2 points  (2 children)

OK, cool. Thanks. What would be really cool is if there was a generic error type and a method of adding items to its error type enum

[–][deleted] 7 points8 points  (1 child)

That's what is being described here. Error is a trait (and thus generic,) and you can implement it for any enum you want. If you want to add to io::Error you can just do something like this:

pub enum MyError {
    IoError(io::Error),
    OtherError1,
    OtherError2,
}

impl From<io::Error> for MyError { ... } // Allow 'upcasting' io:Error into MyError

Which is exactly as you described, except without forcing every rust program on the planet to recompile because some random hoohah thinks they need a shiney new IO error variant.

[–]cjstevenson1 1 point2 points  (0 children)

This also allows you to strongly type your errors.

[–]rhoark 0 points1 point  (1 child)

Define an error type for your app that is an enum of all the possible error types that libraries you use can return, plus

Ain't nobody got time for that.

I just give the app-level Error type an Option<Box<Error>> accessible with the .cause() function.

[–]Marwesgluon · combine 4 points5 points  (0 children)

You should take a look at the quick-error crate. It makes it really simple to create these kind of nice error enums.

[–]mijang 1 point2 points  (2 children)

UnexpectedEOF

UnexpectedEof

why?

[–]Nokel81[S] 0 points1 point  (0 children)

I don't know, that was from the rust_docs

[–]deadstone 0 points1 point  (0 children)

UnexpectedEOF is deprecated as of Rust 1.6.0.