

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Data Leaks
incomplete
2: Sanitizing Error Messages
incomplete
3: Global Error Handling
incomplete
4: Sanitizing Logs
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
A safe error response is useless if one forgotten handler lets a panic escape. In Go, recovery middleware gives you one final boundary between unexpected failures and public responses.
Install a deferred function before calling the next handler. If downstream code panics, recover can capture the value so the middleware can log it and return a generic response:
func recoverPanics(next http.Handler) http.Handler {
return http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
defer func() {
if recovered := recover(); recovered != nil {
slog.Error("unhandled panic", "value", recovered)
http.Error(responseWriter, "Something went wrong", http.StatusInternalServerError)
}
}()
next.ServeHTTP(responseWriter, request)
})
}
Some failures are expected or handler-specific and should be handled where they occur. A missing concert ticket should get a deliberate 404, and invalid checkout input should get a client-safe 400. Recovery middleware is a last resort for unexpected panics, not a replacement for ordinary error handling.
Handle expected failures locally. Use recovery middleware to log unexpected panics and return a safe 500.