This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
json.go
132 lines (115 loc) · 4.27 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
// Copyright 2020 Google LLC
//
// 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 render
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
// RenderJSON renders the interface as JSON. It attempts to gracefully handle
// any rendering errors to avoid partial responses sent to the response by
// writing to a buffer first, then flushing the buffer to the response.
//
// If the provided data is nil and the response code is a 200, the result will
// be `{"ok":true}`. If the code is not a 200, the response will be of the
// format `{"error":"<val>"}` where val is the JSON-escaped http.StatusText for
// the provided code.
//
// If rendering fails, a generic 500 JSON response is returned. In dev mode, the
// error is included in the payload. If flushing the buffer to the response
// fails, an error is logged, but no recovery is attempted.
//
// The buffers are fetched via a sync.Pool to reduce allocations and improve
// performance.
func (r *Renderer) RenderJSON(w http.ResponseWriter, code int, data interface{}) {
// Hello there reader! If you've made it here, you're likely wondering why
// you're getting an error about response codes. For client-interop, it's very
// important that we retain and maintain the allowed list of response codes.
// Adding a new response code requires coordination with the client team so
// they can update their applications to handle that new response code.
if !r.AllowedResponseCode(code) {
r.logger.Errorw("unregistered response code", "code", code)
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
msg := escapeJSON(fmt.Sprintf("%d is not a registered response code", code))
fmt.Fprintf(w, jsonErrTmpl, msg)
return
}
// Avoid marshaling nil data.
if data == nil {
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
// Return an OK response.
if code >= 200 && code < 300 {
fmt.Fprint(w, jsonOKResp)
return
}
// Return an error with the generic HTTP text as the error.
msg := escapeJSON(http.StatusText(code))
fmt.Fprintf(w, jsonErrTmpl, msg)
return
}
// Acquire a renderer
b := r.rendererPool.Get().(*bytes.Buffer)
b.Reset()
defer r.rendererPool.Put(b)
// Render into the renderer
if err := json.NewEncoder(b).Encode(data); err != nil {
msg := "An internal error occurred."
if r.debug {
msg = err.Error()
}
msg = escapeJSON(msg)
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, jsonErrTmpl, msg)
return
}
// Rendering worked, flush to the response
w.WriteHeader(code)
w.Header().Set("Content-Type", "application/json")
if _, err := b.WriteTo(w); err != nil {
// We couldn't write the buffer. We can't change the response header or
// content type if we got this far, so the best option we have is to log the
// error.
r.logger.Errorw("failed to write json to response", "error", err)
}
}
// JSON500 renders the given error as JSON. In production mode, this always
// renders a generic "server error" message. In debug, it returns the actual
// error from the caller.
func (r *Renderer) JSON500(w http.ResponseWriter, err error) {
code := http.StatusInternalServerError
if r.debug {
r.RenderJSON(w, code, map[string]string{
"error": err.Error(),
})
return
}
r.RenderJSON(w, code, map[string]string{
"error": http.StatusText(code),
})
}
// escapeJSON does primitive JSON escaping.
func escapeJSON(s string) string {
return strings.Replace(s, `"`, `\"`, -1)
}
// jsonErrTmpl is the template to use when returning a JSON error. It is
// rendered using Printf, not json.Encode, so values must be escaped by the
// caller.
const jsonErrTmpl = `{"error":"%s"}`
// jsonOKResp is the return value for empty data responses.
const jsonOKResp = `{"ok":true}`