Documentation
¶
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 ¶
package main
import (
"errors"
"fmt"
"net/http"
"github.com/bluekeyes/hatpear"
)
func main() {
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))
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
// RecoverStackDepth is the max depth of stack trace to recover on panic.
RecoverStackDepth = 32
)
Functions ¶
func Get ¶
Get retrieves an error from the request's context. It returns nil if the request was not configured to store errors.
func Store ¶
Store stores an error into the request's context. It panics if the request was not configured to store errors.
Types ¶
type Handler ¶
type Handler interface {
ServeHTTP(w http.ResponseWriter, r *http.Request) error
}
Handler is a variant on http.Handler that can return an error.
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
HandlerFunc is a variant on http.HandlerFunc that can return an 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.
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.
If the panic value is http.ErrAbortHandler, Recover() re-panics with the same value instead of storing a PanicError.
type PanicError ¶
type PanicError struct {
// contains filtered or unexported fields
}
PanicError is an Error created from a recovered panic.
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.