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 ¶
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 ¶
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 ¶
WithHeader adds headers to write when a panic occurs. This option is cumulative: headers from multiple calls will be merged together.
func WithOnRecover ¶
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 ¶
WithStatusCode sets a custom status code to use when a panic occurs.
type RecoverBody ¶
RecoverBody is a custom closure for writing recovery information. By default, DefaultRecoverBody is used.