webhelp.v1: gopkg.in/webhelp.v1/wherr Index | Examples | Files

package wherr

import "gopkg.in/webhelp.v1/wherr"

Package wherr provides a unified error handling framework for http.Handlers.

Code:

package main

import (
    "fmt"
    "net/http"

    "gopkg.in/webhelp.v1/wherr"
    "gopkg.in/webhelp.v1/whlog"
    "gopkg.in/webhelp.v1/whmux"
    "github.com/spacemonkeygo/errors/errhttp"
)

func PageName(r *http.Request) (string, error) {
    if r.FormValue("name") == "" {
        return "", wherr.BadRequest.New("No page name supplied")
    }
    return r.FormValue("name"), nil
}

func Page(w http.ResponseWriter, r *http.Request) {
    name, err := PageName(r)
    if err != nil {
        // This will use our error handler!
        wherr.Handle(w, r, err)
        return
    }

    fmt.Fprintf(w, name)
    // do more stuff
}

func Routes() http.Handler {
    return whmux.Dir{
        "page": http.HandlerFunc(Page),
    }
}

func ErrorHandler(w http.ResponseWriter, r *http.Request, err error) {
    http.Error(w, "some error happened!", errhttp.GetStatusCode(err, 500))
}

func main() {
    // If we didn't register our error handler, we'd end up using a default one.
    whlog.ListenAndServe(":0", wherr.HandleWith(wherr.HandlerFunc(ErrorHandler),
        Routes()))
}

Index

Examples

Package Files

errors.go

Variables

var (
    HTTPError = errors.NewClass("HTTP Error", errors.NoCaptureStack())

    BadRequest                   = ErrorClass(http.StatusBadRequest)
    Unauthorized                 = ErrorClass(http.StatusUnauthorized)
    Forbidden                    = ErrorClass(http.StatusForbidden)
    NotFound                     = ErrorClass(http.StatusNotFound)
    MethodNotAllowed             = ErrorClass(http.StatusMethodNotAllowed)
    NotAcceptable                = ErrorClass(http.StatusNotAcceptable)
    RequestTimeout               = ErrorClass(http.StatusRequestTimeout)
    Conflict                     = ErrorClass(http.StatusConflict)
    Gone                         = ErrorClass(http.StatusGone)
    LengthRequired               = ErrorClass(http.StatusLengthRequired)
    PreconditionFailed           = ErrorClass(http.StatusPreconditionFailed)
    RequestEntityTooLarge        = ErrorClass(http.StatusRequestEntityTooLarge)
    RequestURITooLong            = ErrorClass(http.StatusRequestURITooLong)
    UnsupportedMediaType         = ErrorClass(http.StatusUnsupportedMediaType)
    RequestedRangeNotSatisfiable = ErrorClass(http.StatusRequestedRangeNotSatisfiable)
    ExpectationFailed            = ErrorClass(http.StatusExpectationFailed)
    Teapot                       = ErrorClass(http.StatusTeapot)
    InternalServerError          = ErrorClass(http.StatusInternalServerError)
    NotImplemented               = ErrorClass(http.StatusNotImplemented)
    BadGateway                   = ErrorClass(http.StatusBadGateway)
    ServiceUnavailable           = ErrorClass(http.StatusServiceUnavailable)
    GatewayTimeout               = ErrorClass(http.StatusGatewayTimeout)
)

func ErrorClass Uses

func ErrorClass(code int) *errors.ErrorClass

ErrorClass creates a new subclass of HTTPError using the given HTTP status code

func Handle Uses

func Handle(w http.ResponseWriter, r *http.Request, err error)

Handle uses the provided error handler given via HandleWith to handle the error, falling back to a built in default if not provided.

func HandleWith Uses

func HandleWith(eh Handler, h http.Handler) http.Handler

HandleWith binds the given eror Handler to the request contexts that pass through the given http.Handler. wherr.Handle will use this error Handler for handling errors. If you're using the whfatal package, you should place a whfatal.Catch inside this handler, so this error handler can deal with Fatal requests.

type Handler Uses

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

Handlers handle errors. After HandleError returns, it's assumed a response has been written out and all error handling has completed.

func HandlingWith Uses

func HandlingWith(ctx context.Context) Handler

HandlingWith returns the error handler if registered, or nil if no error handler is registered and the default should be used.

type HandlerFunc Uses

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

func (HandlerFunc) HandleError Uses

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

Package wherr imports 8 packages (graph) and is imported by 11 packages. Updated 2017-06-03. Refresh now. Tools for package owners.