forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
86 lines (73 loc) · 2.47 KB
/
context.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Package app provides application support for context and MongoDB access.
// Current Status Codes:
// 200 OK : StatusOK : Call is success and returning data.
// 204 No Content : StatusNoContent : Call is success and returns no data.
// 400 Bad Request : StatusBadRequest : Invalid post data (syntax or semantics).
// 401 Unauthorized : StatusUnauthorized : Authentication failure.
// 404 Not Found : StatusNotFound : Invalid URL or identifier.
// 500 Internal : StatusInternalServerError : Application specific beyond scope of user.
package app
import (
"encoding/json"
"log"
"net/http"
"gopkg.in/mgo.v2"
)
// Invalid describes a validation error belonging to a specific field.
type Invalid struct {
Fld string `json:"field_name"`
Err string `json:"error"`
}
// jsonError is the response for errors that occur within the API.
type jsonError struct {
Error string `json:"error"`
Fields []Invalid `json:"fields,omitempty"`
}
// Context contains data associated with a single request.
type Context struct {
Session *mgo.Session
http.ResponseWriter
Request *http.Request
Params map[string]string
SessionID string
Status int
}
// Error handles all error responses for the API.
func (c *Context) Error(err error) {
switch err {
case ErrNotFound:
c.RespondError(err.Error(), http.StatusNotFound)
case ErrInvalidID:
c.RespondError(err.Error(), http.StatusBadRequest)
case ErrValidation:
c.RespondError(err.Error(), http.StatusBadRequest)
default:
c.RespondError(err.Error(), http.StatusInternalServerError)
}
}
// Respond sends JSON to the client.
// If code is StatusNoContent, v is expected to be nil.
func (c *Context) Respond(data interface{}, code int) {
log.Printf("%v : api : Respond [%d] : Started", c.SessionID, code)
c.Status = code
if code == http.StatusNoContent {
c.WriteHeader(http.StatusNoContent)
return
}
c.Header().Set("Content-Type", "application/json")
c.WriteHeader(code)
json.NewEncoder(c).Encode(data)
log.Printf("%v : api : Respond [%d] : Completed", c.SessionID, code)
}
// RespondInvalid sends JSON describing field validation errors.
func (c *Context) RespondInvalid(fields []Invalid) {
v := jsonError{
Error: "field validation failure",
Fields: fields,
}
c.Respond(v, http.StatusBadRequest)
}
// RespondError sends JSON describing the error
func (c *Context) RespondError(error string, code int) {
c.Respond(jsonError{Error: error}, code)
}