recovery

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package recovery provides panic recovery middleware for HTTP handlers.

The middleware recovers from panics in HTTP handlers and returns a 500 Internal Server Error response to the client. This prevents a single panicking request from crashing the entire server.

Basic Usage

mux := http.NewServeMux()
mux.HandleFunc("/", handler)
wrappedMux := recovery.Middleware(mux)
http.ListenAndServe(":8080", wrappedMux)

Logging

By default panics are recovered silently. Use WithLogger to log panic details (value, stack trace, request method and path) at ERROR level:

logger := logging.New()
wrappedMux := recovery.Middleware(mux, recovery.WithLogger(logger))

Stability

This package is Beta stability. The API may have minor changes before reaching stable status in v1.0.0.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(next http.Handler, opts ...Option) http.Handler

Middleware is an HTTP middleware that recovers from panics. When a panic occurs, it returns a 500 Internal Server Error response to the client, preventing the panic from crashing the server.

Options can be provided to configure logging behavior. By default, panics are recovered silently. Use WithLogger to enable logging.

Types

type Option added in v0.0.5

type Option func(*config)

Option configures the recovery middleware.

func WithLogger added in v0.0.5

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger used to report recovered panics. When a panic is recovered and a logger is configured, the middleware logs the panic value, stack trace, and request context at ERROR level.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL