recovery

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package recovery implements a middleware around panic recovery.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(opts ...Option) func(http.Handler) http.Handler

NewMiddleware returns a middleware that recovers from [panic]. It's best when used as global & first middleware, as it thus catches any [panic] down the chain.

Example
package main

import (
	"net/http"

	"codeberg.org/kema/kmicro/pkg/http/middleware/recovery"
	"codeberg.org/kema/kmicro/pkg/http/router"
)

func main() {
	r := router.New()
	r.Use(recovery.NewMiddleware())
	// curl localhost:8080/
	r.HandleFunc("GET /", func(_ http.ResponseWriter, _ *http.Request) {
		panic("example recvery")
	})

	_ = http.ListenAndServe("[::]:8080", r.Compile())
}

func WrapHandler

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

WrapHandler wraps h with panic recovery.

Types

type Option

type Option func(*config)

Option configures recovery middleware.

func WithErrorHandler

func WithErrorHandler(errorHandler func(w http.ResponseWriter, err any)) Option

WithErrorHandler sets the panic handler used by the middleware.

Jump to

Keyboard shortcuts

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