-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
106 lines (91 loc) · 3.22 KB
/
response.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package rest
import (
"fmt"
"net/http"
jsoniter "github.com/json-iterator/go"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// ResponseErr is the Response where a straight error cannot be assigned. Simplified for if err != nil
func ResponseErr(w http.ResponseWriter, err error) int {
return Response(w, nil, err, http.StatusOK, "")
}
// Response is a default func to return data
func Response(w http.ResponseWriter, response interface{}, err error, desiredStatus int, location string) int {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Accept", "application/json")
switch err {
case nil:
if location != "" {
w.Header().Set("Location", location)
}
w.WriteHeader(desiredStatus)
if response != nil {
json.NewEncoder(w).Encode(response)
}
return desiredStatus
case ErrConflict:
return ErrorResponse(w, http.StatusConflict, "")
case ErrBadRequest:
return ErrorResponse(w, http.StatusBadRequest, "")
case ErrUnauthorized:
return ErrorResponse(w, http.StatusUnauthorized, "")
case ErrUnprocessableEntity:
return ErrorResponse(w, http.StatusUnprocessableEntity, "")
case ErrNotFound:
return ErrorResponse(w, http.StatusNotFound, "")
}
fmt.Printf("Unknown err: type: %T; value: %q\n", err, err)
return ErrorResponse(w, http.StatusInternalServerError, err.Error())
}
// NotFound is a generic 404 response that will be returned if the router cannot
// match a route
func NotFound(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusNotFound, "")
}
// NotAllowed is a generic 405 response that will be returned if the router can match the method
func NotAllowed(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusMethodNotAllowed, "")
}
// BadRequest is a generic 400 response
func BadRequest(w http.ResponseWriter, r *http.Request, reason string) {
ErrorResponse(w, http.StatusBadRequest, reason)
}
// BadRequestValidation is a generic 400 response describing the validation errors if any
func BadRequestValidation(w http.ResponseWriter, r *http.Request, reason string, validationErrors map[string]string) {
var status int = http.StatusBadRequest
w.WriteHeader(status)
var errResponse HTTPErrorStatus
errResponse.Code = status
errResponse.Message = http.StatusText(status)
if reason != "" {
errResponse.Reason = reason
}
if len(validationErrors) > 0 {
errResponse.ValidationErrors = validationErrors
}
json.NewEncoder(w).Encode(errResponse)
}
// Forbidden is a generic 403 response
func Forbidden(w http.ResponseWriter, r *http.Request, reason string) {
ErrorResponse(w, http.StatusForbidden, reason)
}
// Unauthorized is a generic 401 response
func Unauthorized(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusUnauthorized, "")
}
// UnprocessableEntity is a generic 422 response
func UnprocessableEntity(w http.ResponseWriter, r *http.Request) {
ErrorResponse(w, http.StatusUnprocessableEntity, "")
}
// ErrorResponse returns a formatted error
func ErrorResponse(w http.ResponseWriter, status int, reason string) int {
w.WriteHeader(status)
var err HTTPErrorStatus
err.Code = status
err.Message = http.StatusText(status)
if reason != "" {
err.Reason = reason
}
json.NewEncoder(w).Encode(err)
return status
}