Skip to main content

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 deferred recover()
  • On panic, logs the stack trace via slog.Error
  • Writes 500 Internal Server Error to 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.