-
Notifications
You must be signed in to change notification settings - Fork 207
/
writeHTTP.go
166 lines (144 loc) · 5.71 KB
/
writeHTTP.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package util
import (
"encoding/json"
"fmt"
"net/http"
"github.com/documize/community/core/log"
)
// WritePayloadError error handling
func WritePayloadError(w http.ResponseWriter, method string, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'Bad payload'}"))
log.IfErr(err2)
log.Error(fmt.Sprintf("Unable to decode HTML request body for method %s", method), err)
}
// WriteTransactionError error handling
func WriteTransactionError(w http.ResponseWriter, method string, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
_, err2 := w.Write([]byte("{Error: 'No transaction'}"))
log.IfErr(err2)
log.Error(fmt.Sprintf("Unable to get database transaction for method %s", method), err)
}
// WriteMissingDataError error handling
func WriteMissingDataError(w http.ResponseWriter, method, parameter string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("{Error: 'Missing data'}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Missing data %s for method %s", parameter, method))
}
// WriteNotFoundError error handling
func WriteNotFoundError(w http.ResponseWriter, method string, id string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusNotFound)
_, err := w.Write([]byte("{Error: 'Not found'}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Not found ID %s for method %s", id, method))
}
// WriteGeneralSQLError error handling
func WriteGeneralSQLError(w http.ResponseWriter, method string, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'SQL error'}"))
log.IfErr(err2)
log.Error(fmt.Sprintf("General SQL error for method %s", method), err)
}
// WriteJSONMarshalError error handling
func WriteJSONMarshalError(w http.ResponseWriter, method, entity string, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'JSON marshal failed'}"))
log.IfErr(err2)
log.Error(fmt.Sprintf("Failed to JSON marshal %s for method %s", entity, method), err)
}
// WriteServerError error handling
func WriteServerError(w http.ResponseWriter, method string, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'Internal server error'}"))
log.IfErr(err2)
log.Error(fmt.Sprintf("Internal server error for method %s", method), err)
}
// WriteDuplicateError error handling
func WriteDuplicateError(w http.ResponseWriter, method, entity string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusConflict)
_, err := w.Write([]byte("{Error: 'Duplicate record'}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Duplicate %s record detected for method %s", entity, method))
}
// WriteUnauthorizedError error handling
func WriteUnauthorizedError(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusUnauthorized)
_, err := w.Write([]byte("{Error: 'Unauthorized'}"))
log.IfErr(err)
}
// WriteForbiddenError error handling
func WriteForbiddenError(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusForbidden)
_, err := w.Write([]byte("{Error: 'Forbidden'}"))
log.IfErr(err)
}
// WriteBadRequestError error handling
func WriteBadRequestError(w http.ResponseWriter, method, message string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("{Error: 'Bad Request'}"))
log.IfErr(err)
log.Info(fmt.Sprintf("Bad Request %s for method %s", message, method))
}
// WriteSuccessBytes dumps bytes to HTTP response
func WriteSuccessBytes(w http.ResponseWriter, data []byte) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write(data)
log.IfErr(err)
}
// WriteSuccessString writes string to HTTP response
func WriteSuccessString(w http.ResponseWriter, data string) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(data))
log.IfErr(err)
}
// WriteSuccessEmptyJSON writes empty JSON HTTP response
func WriteSuccessEmptyJSON(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("{}"))
log.IfErr(err)
}
// WriteMarshalError error handling
func WriteMarshalError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
_, err2 := w.Write([]byte("{Error: 'JSON marshal failed'}"))
log.IfErr(err2)
log.Error("Failed to JSON marshal", err)
}
// WriteJSON serializes data as JSON to HTTP response.
func WriteJSON(w http.ResponseWriter, v interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
j, err := json.Marshal(v)
if err != nil {
WriteMarshalError(w, err)
return
}
_, err = w.Write(j)
log.IfErr(err)
}