Wraps Go's http.HandlerFunc, allowing for simpler use of http.NewServeMux().
go get github.com/codingconcepts/errhandlerWrap Go's stdlib http.HandlerFunc using errhandler.Wrap():
mux:=http.NewServeMux()
mux.Handle("POST /products", errhandler.Wrap(addProduct))Update your existing handler signatures, by adding an error return type and utilizing the optional errhandler.ParseJSON and errhandler.SendJSON helper functions to reduce the amount of boilerplate code handlers require:
funcaddProduct(w http.ResponseWriter, r*http.Request) error {
varpproductiferr:=errhandler.ParseJSON(r, &p); err!=nil {
returnerr// Or, if you'd prefer to customise the status code:// return errhandler.Error(http.StatusUnprocessableEntity, err)
}
products[p.ID] =preturnerrhandler.SendJSON(w, p)
}errhandler contains helper objects for building middleware
- A
errhandler.Middlewaretype, which is simply a function that takes a Wrap function and returns a Wrap function:
mux:=http.NewServeMux()
mux.Handle("GET /products/{id}", errhandler.Wrap(midLog(getProduct)))
...funcmidLog(n errhandler.Wrap) errhandler.Wrap {
returnfunc(w http.ResponseWriter, r*http.Request) error {
log.Printf("1 %s %s", r.Method, r.URL.Path)
returnn(w, r)
}
}
funcaddProduct(w http.ResponseWriter, r*http.Request) error {
...
}- A
errhandler.Chainfunction, which allowsMiddlewarefunctions easy to chain:
chain:=errhandler.Chain(midLog1, midLog2)
mux:=http.NewServeMux()
mux.Handle("GET /products/{id}", errhandler.Wrap(chain(getProducts)))
funcmidLog1(n errhandler.Wrap) errhandler.Wrap {
returnfunc(w http.ResponseWriter, r*http.Request) error {
log.Printf("1 %s %s", r.Method, r.URL.Path)
returnn(w, r)
}
}
funcmidLog2(n errhandler.Wrap) errhandler.Wrap {
returnfunc(w http.ResponseWriter, r*http.Request) error {
log.Printf("2 %s %s", r.Method, r.URL.Path)
returnn(w, r)
}
}
funcaddProduct(w http.ResponseWriter, r*http.Request) error {
...
}