Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

We define a custom error type for most functions in the Rust codebase I’m currently working on. It’s a little laborious but not that big a deal and I really enjoy being on the “completely precise” side of the spectrum :).

It still feels lighter weight than Java’s checked exceptions and I think maybe that comes down to all the language machinery that Rust offers around patten matching and error combinators.

I plan to blog about our error handling strategy... someday



Interesting. Although you're the first person I've heard about actually taking that approach, I have heard people lament that Rust library error types tend to be maximal rather than precise (IOW, peeved that a function's declared error type contains errors that that specific function can't actually produce; this is usually a jumping-off point to a spiel about asking Rust to support anonymous sum types), so I'm not completely surprised that somebody out there is actually doing it. That said, I've also heard people on the other side of the spectrum express that Swift's "I don't really care about the specific error type" is what they prefer; notably the anyhow crate (https://docs.rs/anyhow/1.0.32/anyhow/) is a good way to get that behavior in Rust while remaining idiomatic.


I think it is possible to automate some of our approach with a simple macro, I just haven't gotten to it.

I really like that you can look at a function and know every single type of exit it can have and know that you've handled them all.

But this might be because the thing that we are building (replicache.dev) runs in someone else's process so we need to be good citizens and be very careful to never panic or return unexpected/undocumented errors.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: