-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.go
194 lines (174 loc) · 5.69 KB
/
json.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2022 Tomas Machalek <tomas.machalek@gmail.com>
// Copyright 2022 Martin Zimandl <martin.zimandl@gmail.com>
// Copyright 2022 Institute of the Czech National Corpus,
// Faculty of Arts, Charles University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package uniresp
import (
"encoding/json"
"fmt"
"hash/crc32"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
// ActionError represents a basic user action error (e.g. a wrong parameter,
// non-existing record etc.)
type ActionError struct {
error
}
// MarshalJSON serializes the error to JSON
func (me ActionError) MarshalJSON() ([]byte, error) {
return json.Marshal(me.Error())
}
func NewActionErrorFrom(err error) ActionError {
return ActionError{err}
}
// NewActionError creates an Action error from provided message using
// a newly defined general error as the original error
func NewActionError(msg string, args ...any) ActionError {
return ActionError{fmt.Errorf(msg, args...)}
}
// ErrorResponse describes a wrapping object for all error HTTP responses
type ErrorResponse struct {
Error *ActionError `json:"error"`
Details []string `json:"details"`
Code int `json:"code"`
}
type MultiErrorResponse struct {
Errors []string `json:"errors"`
Code int `json:"code"`
}
// WriteJSONResponse writes 'value' to an HTTP response encoded as JSON
func WriteJSONResponse(w http.ResponseWriter, value any) {
jsonAns, err := json.Marshal(value)
if err != nil {
log.Err(err).Msg("failed to encode a result to JSON")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(jsonAns)
}
// WriteJSONResponseWithStatus writes 'value' to an HTTP response encoded as JSON
func WriteJSONResponseWithStatus(w http.ResponseWriter, status int, value any) {
jsonAns, err := json.Marshal(value)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(status)
w.Write(jsonAns)
}
func testEtagValues(headerValue, testValue string) bool {
if headerValue == "" {
return false
}
for _, item := range strings.Split(headerValue, ", ") {
if strings.HasPrefix(item, "\"") && strings.HasSuffix(item, "\"") {
val := item[1 : len(item)-1]
if val == testValue {
return true
}
} else {
log.Warn().Msgf("Invalid ETag value: %s", item)
}
}
return false
}
// WriteCacheableJSONResponse writes 'value' to an HTTP response encoded as JSON
// but before doing that it calculates a checksum of the JSON and in case it is
// equal to provided 'If-Match' header, 304 is returned. Otherwise a value with
// ETag header is returned.
func WriteCacheableJSONResponse(w http.ResponseWriter, req *http.Request, value any) {
jsonAns, err := json.Marshal(value)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d", 3600*24*30))
crc := crc32.ChecksumIEEE(jsonAns)
newEtag := fmt.Sprintf("chksm-%d", crc)
reqEtagString := req.Header.Get("If-Match")
if testEtagValues(reqEtagString, newEtag) {
http.Error(w, http.StatusText(http.StatusNotModified), http.StatusNotModified)
} else {
w.Header().Set("Etag", newEtag)
w.Write(jsonAns)
}
}
}
// RespondWithErrorJSON is currently the preferred
// way how to write error responses. Compared with
// `WriteJSONErrorResponse` it accepts any type of
// error but it is able to detect `ActionError` and
// use it accordingly. It also attaches the error
// to Gin's Context.
func RespondWithErrorJSON(ctx *gin.Context, err error, status int) {
aerr, ok := err.(ActionError)
if !ok {
aerr = NewActionErrorFrom(err)
}
ctx.Error(aerr)
WriteJSONErrorResponse(ctx.Writer, aerr, status)
}
// WriteJSONErrorResponse writes 'aerr' to an HTTP error response as JSON
// Please note that in most cases, the `RespondWithErrorJSON` is preferred.
func WriteJSONErrorResponse(w http.ResponseWriter, aerr ActionError, status int, details ...string) {
var errStr *string
if aerr.Error() != "" {
tmp := aerr.Error()
errStr = &tmp
}
jsonAns, err := json.Marshal(struct {
Code int `json:"code"`
Error *string `json:"error"`
Details []string `json:"details"`
}{
Code: status,
Error: errStr,
Details: details,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.WriteHeader(status)
w.Write(jsonAns)
}
func WriteJSONMultiErrorResponse(w http.ResponseWriter, errors []error, status int) {
errMsgs := make([]string, len(errors))
for i, e := range errors {
errMsgs[i] = e.Error()
}
ans := &MultiErrorResponse{
Code: status,
Errors: errMsgs,
}
jsonAns, err := json.Marshal(ans)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.WriteHeader(status)
w.Write(jsonAns)
}
// WriteCustonmJSONErrorResponse writes any JSON serializable object as an HTTP error response.
// In case the value cannot be serialized into JSON, the function will write error
// 500 (Internal Server Error).
func WriteCustomJSONErrorResponse(w http.ResponseWriter, value any, status int, details ...string) {
jsonAns, err := json.Marshal(value)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.WriteHeader(status)
w.Write(jsonAns)
}