recovery

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2022 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package recovery implements an http.Handler that recovers from panics, allowing configurable actions to take when a panic occurs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultRecoverBody

func DefaultRecoverBody(w io.Writer, r interface{}, stack []byte)

DefaultRecoverBody is the default strategy for writing the recovery argument. This function writes a string representation of r, followed by the stack trace.

func Middleware

func Middleware(options ...Option) func(http.Handler) http.Handler

Middleware creates a http.Handler decorator that recovers any panics from downstream handlers.

Decorated handlers created by the returned middleware will catch all panics and attempt to write a useful HTTP message. By default, http.StatusInternalServerError will be set as the status, and the recovery object passed to panic will be output along with a debug stack trace. The options can be used to customize this behavior.

Note that if any handlers wrote information to the HTTP response before the panic, the decorator may not be able to write panic information. For example, if WriteHeader was called prior to panicking, then the decorator cannot set a status code.

In addition to HTTP output, one or more OnRecover strategies can be added via options that will be called with the recovery object and debug stack. This can be used to hook in logging, metrics, etc.

Types

type OnRecover

type OnRecover func(r interface{}, stack []byte)

OnRecover is a callback that receives information about a recovery object. Both the argument passed to panic and the debug stack trace are passed to this closure.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is a configurable option for a recovery decorator.

func WithHeader

func WithHeader(h httpaux.Header) Option

WithHeader adds headers to write when a panic occurs. This option is cumulative: headers from multiple calls will be merged together.

func WithOnRecover

func WithOnRecover(f ...OnRecover) Option

WithOnRecover adds zero or more OnRecover callbacks to the middleware.

func WithRecoverBody

func WithRecoverBody(rb RecoverBody) Option

WithRecoverBody adds a custom RecoverBody strategy for writing out recover objects.

func WithStatusCode

func WithStatusCode(sc int) Option

WithStatusCode sets a custom status code to use when a panic occurs.

type RecoverBody

type RecoverBody func(w io.Writer, r interface{}, stack []byte)

RecoverBody is a custom closure for writing recovery information. By default, DefaultRecoverBody is used.

Jump to

Keyboard shortcuts

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