Layered Error Handling in Go
When I started building HTTP services in Go, my error handling was a mess — and I didn’t notice until the project got big enough to hurt. Every handler decided its own HTTP status. One endpoint returned 400 for a missing record; another returned 404 for the same thing. Validation errors came back in the raw shape of the validator library. My repository returned plain errors.New("not found") strings that the handler had to pattern-match. And one memorable afternoon I found a raw Postgres error — table names and all — sitting in a JSON response I’d shipped to the frontend. ...