hatpear

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 5 Imported by: 4

README

hatpear GoDoc

hatpear (from "httperr") is a simple, unopinionated Go 1.7+ library for capturing and responding to errors that occur while handling HTTP requests. It has no dependencies and works well with the standard library or other HTTP libraries that use standard types.

See the package documentation for examples and usage details.

Stability Note: While the API is simple, it hasn't seen heavy use yet and may change in the future. I recommend vendoring this package at a specific commit if you are concerned about API changes.

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

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

Functions

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 added in v0.1.1

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.

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

type Middleware func(http.Handler) http.Handler

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.

Jump to

Keyboard shortcuts

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