-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
63 lines (48 loc) · 1.85 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Package jh implements a shim layer between functions that return values and errors and the httprouter.Handle function signature.
This package allows us to write http handler functions that return concrete values, for example:
func (srv *server) handle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) (interface{}, error) {
v, err := db()
if err != nil {
return nil, Wrap(err, "db")
}
return map[string]string{"val": v}, nil
}
The Adapter function takes these return values and appropriately sets headers,
and converts the empty interface into a json repsonse.
There are three reasons this package exists. First of all it allows functions
to divorce themselves from the JSON encoding and header setting. Secondly in
returning errors it reduces cases where the explicit return is forgotten in
cases such as:
if err != nil {
http.Error(w, "this is a failure", http.StatusInternalServerError)
}
// oops, this shouldn't be reached.
Lastly it allows code called by the http view to set status code where
appropriate. For example consider the following database code:
func insert(id string, val string) error {
if find(id){
return NewError("id already exists", http.StatusConflict)
}
// do insert
}
func update(id, val string) error {
if !find(id){
return NewError("id not found", http.StatusNotFound)
}
if err := db.Update(id, val); err != nil {
return errors.Wrap(err, "db update")
}
}
The calling function can have the appropriate headers set by using the Adapter function:
func (srv *server) update(w http.ResponseWriter, r *http.Request, ps httprouter.Params) (interface{}, error) {
// parse json update from body
err := update(val)
if err != nil {
return nil, Wrap(err, "update failure")
}
// ...
}
Which would return the appropriate http status code according to the underlying error.
*/
package jh