Recover
Panic recovery middleware. Catches panics in handlers and middleware chains, logs the stack trace, and returns a 500 response - preventing the server from crashing.
Usage
r.Use(middleware.Recover)
How It Works
- Calls
c.Next()inside a deferredrecover() - On panic, logs the stack trace via
slog.Error - Writes
500 Internal Server Errorto the response - The server continues serving subsequent requests normally
Recover should typically be the first middleware in the chain to catch panics from all downstream middleware and handlers.