5 ms·
Improved Error Handling in Rust
- arthursilva 12y agoI like it. Simple and extensible, if you want to.
- mercurial 12y agoSo it means the client of your library can simply pattern-match on the result returned by cause() to find out if it was caused by, say, FileNotFoundException? Nice.
- masklinn 12y agoNope, cause() returns an &Error, there's nothing you can pattern match there. And despite the documentation's claim, as far as I can see Error does not extend Any, which would be necessary for dynamic typechecking. Also remember that cause() is recursive, you may need to traverse the whole chain before finding what you're looking for (not that you can find it at the moment)
- steveklabnik 12y ago> And despite the documentation's claim, as far as I can see Error does not extend Any, which would be necessary for dynamic typechecking. See http://www.reddit.com/r/rust/comments/2l98pn/error_interoperation_now_available_in_the/clssbf1?context=1 http://www.reddit.com/r/rust/comments/2l98pn/error_interoper...
- mercurial 12y agoI don't think that cause() being recursive is an issue, but I'm not sure why you'd want the cause if you can't do anything useful like attempt to downcast it.
- comex 12y agoYou don't need to use cause(). For the LibError example, I think you'd instead want to pattern match against the actual variants defined in the enum, one of which is IoError(io::IoError), at which point you could match the IoError's kind against FileNotFound. This way you're depending on the published contract of the libraries in question, rather than the fact that you'll be given an error whose eventual cause is some file somewhere not being found, which is more of an implementation detail.
- mercurial 12y agoThat's a good idea in general, but there are times where you do need downcasting. Maybe your generic DB framework can throw a specific error on MySQL that you known how to recover from, but which is only available from cause() but not from the library's contract.
- masklinn 12y ago> but I'm not sure why you'd want the cause if you can't do anything useful like attempt to downcast it. description() and detail() are available, so it lets you display a traceback of sorts and do basic root-cause analysis.
- NateDad 12y agoAs a gopher who has written an errors package for Go, I find Rust's journey into figuring out errors to be very interesting. A lot of the stuff in this article is similar to many of the error patterns in Go. Of course, with Go, it's a lot more ad-hoc and less boilerplate, but also obviously fewer compile-time guarantees. The try! macro is something I know a lot of people would love to have in Go (those pesky if err != nil { return err } lines really bother people for some reason). I think it's interesting that the built-in error type has a cause, that seems to be one of the most common patterns in Go error packages as well. Is there any automatic stack tracing? That seems to be one other common thing in Go error packages.
- Dewie 12y ago> Of course, with Go, it's a lot more ad-hoc and less boilerplate, How is it less boilerplate?
- arthursilva 12y agoThat's what I though, since in Go you also need to declare your error struct and implement the error interface on it.
- jerf 12y agoWhich is not something you generally do a lot of. Most functions either pass an error through or simply return a string error through errors.New(). (That's often not a cheat, either; my modules tend to declare the set of errors as globally-exported variables, allowing you to use == to figure out which error you're getting if you care. If your errors don't take parameters that works fine.) And the error interface only has one method instead of three. Less boilerplate in the worst and the common cases, less compile time guarantees. It is debatable whether passing errors through is a good idea in theory or in practice at scale (two separate questions there). So far it hasn't bit me at all, though, and I've got a good idea of what I'm looking for for such "bitings". I find in practice there's two types of errors: Errors I know how to do something about, and errors I don't. In the first case, I can only be dealing with a certain restricted set of errors coming back and I must inevitably write special-purpose code for the given error, and in the second, I don't much care about what it is so I often send it along untouched. I haven't found anything that manages to split the difference yet. Even returning a wrapped error is rare in my application code, though it has its place in library code. I'm also finding in my real code that the ratio turns out to be closer to 1:2 errors I can do something with/errors I can't, which makes the explicit handling not matter so much. (My gut feeling is that if you find you are always just "return err"ering, and especially if you are doing it without thought, you're writing code that is passing up on some robustness.)