3 ms·
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?
by ThePhysicist 2y 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?
- supriyo-biswas 2y agoThe 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/recoverer.go https://github.com/go-chi/chi/blob/master/middleware/recover...
- smnscu 2y agoHaving 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.
- commandersaki 2y agoI've seen panic/recover used a lot with recursive descent parsers.
- rgallagher27 2y agoI'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.
- colonial 2y agoAt 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.
- lelandbatey 2y agoI've only ever had to use it in certain complex cleanup mechanisms (certain transaction handling, request middleware, etc)
- goodoldneon 2y agoI 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