-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjson.go
186 lines (154 loc) · 5.11 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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package herodot
import (
"bytes"
"context"
"encoding/json"
stderr "errors"
"net/http"
"github.com/pkg/errors"
)
type ErrorContainer struct {
Error *DefaultError `json:"error"`
}
type ErrorReporter interface {
ReportError(r *http.Request, code int, err error, args ...interface{})
}
type EncoderOptions func(*json.Encoder)
// UnescapedHTML prevents HTML entities &, <, > from being unicode-escaped.
func UnescapedHTML(enc *json.Encoder) {
enc.SetEscapeHTML(false)
}
// JSONWriter writes JSON responses (obviously).
type JSONWriter struct {
Reporter ErrorReporter
ErrorEnhancer func(r *http.Request, err error) interface{}
EnableDebug bool
}
var _ Writer = (*JSONWriter)(nil)
func NewJSONWriter(reporter ErrorReporter) *JSONWriter {
writer := &JSONWriter{
Reporter: reporter,
ErrorEnhancer: defaultJSONErrorEnhancer,
}
if writer.Reporter == nil {
writer.Reporter = &stdReporter{}
}
writer.ErrorEnhancer = defaultJSONErrorEnhancer
return writer
}
type ErrorEnhancer interface {
EnhanceJSONError() interface{}
}
func defaultJSONErrorEnhancer(r *http.Request, err error) interface{} {
if e, ok := err.(ErrorEnhancer); ok {
return e.EnhanceJSONError()
}
return &ErrorContainer{Error: ToDefaultError(err, r.Header.Get("X-Request-ID"))}
}
func Scrub5xxJSONErrorEnhancer(r *http.Request, err error) interface{} {
payload := defaultJSONErrorEnhancer(r, err)
if de, ok := payload.(DefaultError); ok {
if de.StatusCode() >= 500 {
return scrub5xxError(&de)
}
return payload
}
if ec, ok := payload.(*ErrorContainer); ok {
if ec.Error.CodeField >= 500 {
return scrub5xxError(ec.Error)
}
return payload
}
// We have some other error, which we always want to scrub.
return &ErrorContainer{Error: ToDefaultError(&ErrInternalServerError, r.Header.Get("X-Request-ID"))}
}
func scrub5xxError(err *DefaultError) *ErrorContainer {
return &ErrorContainer{Error: &DefaultError{
IDField: err.IDField,
CodeField: err.CodeField,
StatusField: err.StatusField,
RIDField: err.RIDField,
GRPCCodeField: err.GRPCCodeField,
}}
}
// Write a response object to the ResponseWriter with status code 200.
func (h *JSONWriter) Write(w http.ResponseWriter, r *http.Request, e interface{}, opts ...EncoderOptions) {
h.WriteCode(w, r, http.StatusOK, e, opts...)
}
// WriteCode writes a response object to the ResponseWriter and sets a response code.
func (h *JSONWriter) WriteCode(w http.ResponseWriter, r *http.Request, code int, e interface{}, opts ...EncoderOptions) {
bs := new(bytes.Buffer)
enc := json.NewEncoder(bs)
for _, opt := range opts {
opt(enc)
}
err := enc.Encode(e)
if err != nil {
h.WriteError(w, r, errors.WithStack(err))
return
}
if code == 0 {
code = http.StatusOK
}
if errors.Is(r.Context().Err(), context.Canceled) {
code = StatusClientClosedRequest
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
_, _ = w.Write(bs.Bytes())
}
// WriteCreated writes a response object to the ResponseWriter with status code 201 and
// the Location header set to location.
func (h *JSONWriter) WriteCreated(w http.ResponseWriter, r *http.Request, location string, e interface{}) {
w.Header().Set("Location", location)
h.WriteCode(w, r, http.StatusCreated, e)
}
// WriteError writes an error to ResponseWriter and tries to extract the error's status code by
// asserting statusCodeCarrier. If the error does not implement statusCodeCarrier, the status code
// is set to 500.
func (h *JSONWriter) WriteError(w http.ResponseWriter, r *http.Request, err error, opts ...Option) {
if c := StatusCodeCarrier(nil); stderr.As(err, &c) {
h.WriteErrorCode(w, r, c.StatusCode(), err)
} else {
h.WriteErrorCode(w, r, http.StatusInternalServerError, err, opts...)
}
}
// WriteErrorCode writes an error to ResponseWriter and forces an error code.
func (h *JSONWriter) WriteErrorCode(w http.ResponseWriter, r *http.Request, code int, err error, opts ...Option) {
o := newOptions(opts)
if code == 0 {
code = http.StatusInternalServerError
}
if errors.Is(r.Context().Err(), context.Canceled) {
code = StatusClientClosedRequest
}
if !o.noLog {
// All errors land here, so it's a really good idea to do the logging here as well!
h.Reporter.ReportError(r, code, coalesceError(err), "An error occurred while handling a request")
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
// Enhancing must happen after logging or context will be lost.
var payload interface{} = err
if h.ErrorEnhancer != nil {
payload = h.ErrorEnhancer(r, err)
}
if de, ok := payload.(*DefaultError); ok && !h.EnableDebug {
de2 := *de
de2.DebugField = ""
payload = &de2
}
if ec, ok := payload.(*ErrorContainer); ok && !h.EnableDebug {
de2 := *ec.Error
de2.DebugField = ""
ec2 := *ec
ec2.Error = &de2
payload = ec2
}
if err := json.NewEncoder(w).Encode(payload); err != nil {
// There was an error, but there's actually not a lot we can do except log that this happened.
h.Reporter.ReportError(r, code, errors.WithStack(err), "Could not write ErrorContainer to response writer")
}
}