...

Package hatpear

import "github.com/bluekeyes/hatpear"
Overview
Index
Examples

Overview ▾

Package hatpear provides a way to aggregate errors from HTTP handlers so they can be processed by middleware. Errors are stored in the context of the current request either manually, when using standard library handler types, or automatically, when using this package's handler types.

Using the middleware returned by the Catch function is required for this package to work; usage of all other functions and types is optional.

Example

Code:

std := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    err := errors.New("this failed!")
    hatpear.Store(r, err)
})

pear := hatpear.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
    return errors.New("this also failed!")
})

mux := http.NewServeMux()
mux.Handle("/std", std)
mux.Handle("/hatpear", hatpear.Try(pear))

catch := hatpear.Catch(func(w http.ResponseWriter, r *http.Request, err error) {
    fmt.Printf("[ERROR]: %s\n", err.Error())
    http.Error(w, err.Error(), http.StatusInternalServerError)
})

http.ListenAndServe(":8000", catch(mux))

Variables

var (
    // RecoverStackDepth is the max depth of stack trace to recover on panic.
    RecoverStackDepth = 32
)

func Get

func Get(r *http.Request) error

Get retrieves an error from the request's context. It returns nil if the request was not configured to store errors.

func Store

func Store(r *http.Request, err error)

Store stores an error into the request's context. It panics if the request was not configured to store errors.

func Try

func Try(h Handler) http.Handler

Try converts a handler to a standard http.Handler, storing any error in the request's context.

func TryFunc

func TryFunc(h func(http.ResponseWriter, *http.Request) error) http.Handler

TryFunc converts a handler function to a standard http.Handler, storing any error in the request's context.

type Handler

Handler is a variant on http.Handler that can return an error.

type Handler interface {
    ServeHTTP(w http.ResponseWriter, r *http.Request) error
}

type HandlerFunc

HandlerFunc is a variant on http.HandlerFunc that can return an error.

type HandlerFunc func(w http.ResponseWriter, r *http.Request) error

func (HandlerFunc) ServeHTTP

func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error

type Middleware

Middleware adds additional functionality to an existing handler.

type Middleware func(http.Handler) http.Handler

func Catch

func Catch(h func(w http.ResponseWriter, r *http.Request, err error)) Middleware

Catch creates middleware that processes errors stored while serving a request. Errors are passed to the callback, which should write them to the response in an appropriate format. This is usually the outermost middleware in a chain.

func Recover

func Recover() Middleware

Recover creates middleware that can recover from a panic in a handler, storing a PanicError for future handling.

type PanicError

PanicError is an Error created from a recovered panic.

type PanicError struct {
    // contains filtered or unexported fields
}

func (PanicError) Error

func (e PanicError) Error() string

func (PanicError) Format

func (e PanicError) Format(s fmt.State, verb rune)

Format formats the error optionally including the stack trace.

%s    the error message
%v    the error message and the source file and line number for each stack frame

Format accepts the following flags:

%+v   the error message, and the function, file, and line for each stack frame

func (PanicError) StackTrace

func (e PanicError) StackTrace() []runtime.Frame

StackTrace returns the stack of the panicking goroutine.

func (PanicError) Value

func (e PanicError) Value() interface{}

Value returns the exact value with which panic() was called.