I made a library for streamlined error handling and piping through a queue of Go functions.
You can find it here: https://github.com/go-on/queue
It has a compact and a verbose syntactic variant.
Here is an example for the short syntax:
import "github.com/go-on/queue/q"
func SaveUser(w http.ResponseWriter, rq *http.Request) {
u := &User{}
err := q.Q(
ioutil.ReadAll, rq.Body, // read json (returns json and error)
)(
// q.V pipes the json from the previous function call
json.Unmarshal, q.V, u, // unmarshal json from above (returns error)
)(
u.Validate, // validate the user (returns error)
)(
u.Save, // save the user (returns error)
)(
ok, w, // send the "ok" message (returns no error)
).Run()
if err != nil {
switch err {
case *json.SyntaxError:
...
}
}
}
Please be aware that there is a little performance overhead, since it
makes use of reflection.
Also this is not idiomatic go code, so you will want to use it in your own projects, or if your team agrees on
using it.