recover

package
v4.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package recover provides panic recovery utilities for HTTP handlers.

Contract:

  • if the response is still uncommitted, a recovered panic becomes a 500 Problem Details response
  • if headers or body bytes have already been committed, the middleware logs the panic and aborts the request instead of preserving a misleading partial success response

Purpose: See the package summary above. Import: `github.com/aatuh/api-toolkit/v4/httpx/recover`. Example: See docs/api-reference.md for package example links and docs/cookbook.md for task recipes. Errors: Constructors, parsers, and handlers return or write documented errors according to their signatures; packages with plain data types do not add hidden error channels. Concurrency: Treat configured middleware and helpers as immutable after construction; request and response values remain request-scoped unless a type documents stronger guarantees. Stability: Stable core API under VERSIONING.md and scripts/apicheck.sh. When not to use: Prefer net/http, application-owned types, or narrower helpers when this package contract is not needed.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware() func(http.Handler) http.Handler

Middleware converts panics into RFC 9457 problem details responses when the response is still uncommitted. If the handler already committed headers or body bytes, the middleware logs the panic and aborts the request rather than preserving a misleading partial success response.

func New

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

New converts uncommitted panics into RFC 9457 problem details responses and logs them through a configurable logger. Once a response has been committed, recovery logs the panic and aborts the request instead of returning a partial success response.

Example
package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"

	recovermw "github.com/aatuh/api-toolkit/v4/httpx/recover"
	"github.com/aatuh/api-toolkit/v4/ports"
)

func main() {
	middleware := recovermw.New(recovermw.WithLogger(ports.NopLogger{}), recovermw.WithStackLogging(false))
	handler := middleware(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
		panic("boom")
	}))

	recorder := httptest.NewRecorder()
	req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
	handler.ServeHTTP(recorder, req)

	fmt.Println(recorder.Code)

}
Output:
500

Types

type Option

type Option func(*config)

Option customizes panic recovery logging.

func WithLogger

func WithLogger(log ports.Logger) Option

WithLogger routes panic logs to the provided logger.

func WithStackLogging

func WithStackLogging(enabled bool) Option

WithStackLogging controls whether panic stacks are added to logs.

Jump to

Keyboard shortcuts

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