Hacker News new | ask | show | jobs
by ThePhysicist 475 days ago
Do you use recover() a lot? I have never used it much, I guess it is important in some cases but I don't think it's used that much in practice, or is it?
7 comments

The only use for me has been to put a recoverer middleware[1] to catch any unhandled panics and return HTTP 500s in my applications.

[1] https://github.com/go-chi/chi/blob/master/middleware/recover...

Having used Go professionally for over a decade, I can count on one hand the times I used recover(). I've actually just refactored some legacy code last week to remove a panic/recover that was bafflingly used to handle nil values. The only valid use case I can think of is gracefully shutting down a server, but that's usually addressed by some library.
I've "used" it in pretty much every Go project I've worked on but almost always in the form of an HTTP handle middleware. Write once, maybe update once a year when we have a change to how we report/log errors.
At least in Rust (which has an effectively identical API here) the only reasonable use case I've seen is as a "last resort" in long-running programs to transform panicking requests into a HTTP 500 or equivalent.
I always make sure recover is used in all goroutines I start. I don’t want a panic in a random goroutine to crash my whole server
I've only ever had to use it in certain complex cleanup mechanisms (certain transaction handling, request middleware, etc)
I've seen panic/recover used a lot with recursive descent parsers.