Documentation ¶
Overview ¶
Package ehttprouter wraps httprouter with an error management middleware.
Example (Panic) ¶
package main import ( "log" "net/http" "github.com/creack/ehttp" "github.com/creack/ehttp/ehttprouter" "github.com/julienschmidt/httprouter" ) func main() { hdlr := func(w http.ResponseWriter, req *http.Request, p httprouter.Params) error { panic(ehttp.NewErrorf(http.StatusTeapot, "fail")) } router := httprouter.New() router.GET("/", ehttprouter.MWErrorPanic(hdlr)) log.Fatal(http.ListenAndServe(":8080", router)) }
Output:
Example (Simple) ¶
package main import ( "log" "net/http" "github.com/creack/ehttp" "github.com/creack/ehttp/ehttprouter" "github.com/julienschmidt/httprouter" ) func main() { hdlr := func(w http.ResponseWriter, req *http.Request, p httprouter.Params) error { return ehttp.NewErrorf(http.StatusTeapot, "fail") } router := httprouter.New() router.GET("/", ehttprouter.MWError(hdlr)) log.Fatal(http.ListenAndServe(":8080", router)) }
Output:
Index ¶
- Variables
- func MWError(handle Handle) httprouter.Handle
- func MWErrorPanic(handle Handle) httprouter.Handle
- type Handle
- type Router
- func (r *Router) DELETE(path string, handle Handle)
- func (r *Router) GET(path string, handle Handle)
- func (r *Router) HEAD(path string, handle Handle)
- func (r *Router) Handle(method, path string, handle Handle)
- func (r *Router) Handler(method string, path string, handler http.Handler)
- func (r *Router) HandlerFunc(method string, path string, handler http.HandlerFunc)
- func (r *Router) Lookup(method string, path string) (httprouter.Handle, httprouter.Params, bool)
- func (r *Router) MWError(handle func(http.ResponseWriter, *http.Request, httprouter.Params) error) httprouter.Handle
- func (r *Router) MWErrorPanic(handle Handle) httprouter.Handle
- func (r *Router) OPTIONS(path string, handle Handle)
- func (r *Router) PATCH(path string, handle Handle)
- func (r *Router) POST(path string, handle Handle)
- func (r *Router) PUT(path string, handle Handle)
- func (r *Router) PanicHandler()
- func (r *Router) ServeFiles(path string, root http.FileSystem)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultRouter = &Router{ Router: httprouter.New(), mux: ehttp.DefaultServeMux, recoverPanic: false, }
DefaultRouter is the default router for direct access.
Functions ¶
func MWError ¶
func MWError(handle Handle) httprouter.Handle
MWError exposes the default router MWError method.
func MWErrorPanic ¶
func MWErrorPanic(handle Handle) httprouter.Handle
MWErrorPanic exposes the default router MWErrorPanic method.
Types ¶
type Handle ¶
type Handle func(http.ResponseWriter, *http.Request, httprouter.Params) error
Handle wraps httprouter's Handle and extends it with an error return.
type Router ¶ added in v0.4.1
type Router struct { *httprouter.Router // Underlying httprouter.Router. // contains filtered or unexported fields }
Router wraps *github.com/julienschmidt/httprouter.Router with error management.
func New ¶ added in v0.4.1
func New(sendErrorCallback func(ehttp.ResponseWriter, *http.Request, error), errorContentType string, recoverPanic bool, logger *log.Logger) *Router
New instantiates a new ehttprouter.Router.
func (*Router) Handler ¶ added in v0.4.1
Handler exposes the httprouter Handler method. NOTE: does not handle erors nor panics.
func (*Router) HandlerFunc ¶ added in v0.4.1
func (r *Router) HandlerFunc(method string, path string, handler http.HandlerFunc)
HandlerFunc exposes the httprouter HandlerFunc method. NOTE: does not handle erors nor panics.
func (*Router) Lookup ¶ added in v0.4.1
func (r *Router) Lookup(method string, path string) (httprouter.Handle, httprouter.Params, bool)
Lookup exposes the httprouter Lookup method. NOTE: Return the wrapped handler.
func (*Router) MWError ¶ added in v0.4.1
func (r *Router) MWError(handle func(http.ResponseWriter, *http.Request, httprouter.Params) error) httprouter.Handle
MWError is the middleware taking care of the returned error.
func (*Router) MWErrorPanic ¶ added in v0.4.1
func (r *Router) MWErrorPanic(handle Handle) httprouter.Handle
MWErrorPanic wraps MWError and recovers from panic.
func (*Router) PanicHandler ¶ added in v0.4.2
func (r *Router) PanicHandler()
PanicHandler is a place holder to disable unwanted access to the underlying field. Panic is handled via ehttprouter instead.
func (*Router) ServeFiles ¶ added in v0.4.1
func (r *Router) ServeFiles(path string, root http.FileSystem)
ServeFiles exposes the httprouter ServeFiles method. NOTE: does not handle errors nor panics.