Codebase list golang-github-go-kit-kit / cb67d82
jsonrpc.DefaultErrorEncoder: add RequestID in error body (#969) * Add RequestID in error body * Implementing review suggestions Lorenzo authored 4 years ago GitHub committed 4 years ago
2 changed file(s) with 57 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
99 "github.com/go-kit/kit/log"
1010 httptransport "github.com/go-kit/kit/transport/http"
1111 )
12
13 type requestIDKeyType struct{}
14
15 var requestIDKey requestIDKeyType
1216
1317 // Server wraps an endpoint and implements http.Handler.
1418 type Server struct {
104108 return
105109 }
106110
111 ctx = context.WithValue(ctx, requestIDKey, req.ID)
112
107113 // Get the endpoint and codecs from the map using the method
108114 // defined in the JSON object
109115 ecm, ok := s.ecm[req.Method]
159165 // If the error implements ErrorCoder, the provided code will be set on the
160166 // response error.
161167 // If the error implements Headerer, the given headers will be set.
162 func DefaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter) {
168 func DefaultErrorEncoder(ctx context.Context, err error, w http.ResponseWriter) {
163169 w.Header().Set("Content-Type", ContentType)
164170 if headerer, ok := err.(httptransport.Headerer); ok {
165171 for k := range headerer.Headers() {
176182 }
177183
178184 w.WriteHeader(http.StatusOK)
185
186 var requestID *RequestID
187 if v := ctx.Value(requestIDKey); v != nil {
188 requestID = v.(*RequestID)
189 }
179190 _ = json.NewEncoder(w).Encode(Response{
191 ID: requestID,
180192 JSONRPC: Version,
181193 Error: &e,
182194 })
2323 return strings.NewReader(in)
2424 }
2525
26 func unmarshalResponse(body []byte) (resp jsonrpc.Response, err error) {
27 err = json.Unmarshal(body, &resp)
28 return
29 }
30
2631 func expectErrorCode(t *testing.T, want int, body []byte) {
27 var r jsonrpc.Response
28 err := json.Unmarshal(body, &r)
29 if err != nil {
30 t.Fatalf("Cant' decode response. err=%s, body=%s", err, body)
32 t.Helper()
33
34 r, err := unmarshalResponse(body)
35 if err != nil {
36 t.Fatalf("Can't decode response: %v (%s)", err, body)
3137 }
3238 if r.Error == nil {
3339 t.Fatalf("Expected error on response. Got none: %s", body)
3440 }
3541 if have := r.Error.Code; want != have {
3642 t.Fatalf("Unexpected error code. Want %d, have %d: %s", want, have, body)
43 }
44 }
45
46 func expectValidRequestID(t *testing.T, want int, body []byte) {
47 t.Helper()
48
49 r, err := unmarshalResponse(body)
50 if err != nil {
51 t.Fatalf("Can't decode response: %v (%s)", err, body)
52 }
53 have, err := r.ID.Int()
54 if err != nil {
55 t.Fatalf("Can't get requestID in response. err=%s, body=%s", err, body)
56 }
57 if want != have {
58 t.Fatalf("Request ID: want %d, have %d (%s)", want, have, body)
59 }
60 }
61
62 func expectNilRequestID(t *testing.T, body []byte) {
63 t.Helper()
64
65 r, err := unmarshalResponse(body)
66 if err != nil {
67 t.Fatalf("Can't decode response: %v (%s)", err, body)
68 }
69 if r.ID != nil {
70 t.Fatalf("Request ID: want nil, have %v", r.ID)
3771 }
3872 }
3973
91125 }
92126 buf, _ := ioutil.ReadAll(resp.Body)
93127 expectErrorCode(t, jsonrpc.InternalError, buf)
128 expectValidRequestID(t, 1, buf)
94129 }
95130
96131 func TestServerBadEncode(t *testing.T) {
110145 }
111146 buf, _ := ioutil.ReadAll(resp.Body)
112147 expectErrorCode(t, jsonrpc.InternalError, buf)
148 expectValidRequestID(t, 1, buf)
113149 }
114150
115151 func TestServerErrorEncoder(t *testing.T) {
161197 }
162198 buf, _ := ioutil.ReadAll(resp.Body)
163199 expectErrorCode(t, jsonrpc.ParseError, buf)
200 expectNilRequestID(t, buf)
164201 }
165202
166203 func TestServerUnregisteredMethod(t *testing.T) {
185222 if want, have := http.StatusOK, resp.StatusCode; want != have {
186223 t.Errorf("want %d, have %d (%s)", want, have, buf)
187224 }
188 var r jsonrpc.Response
189 err := json.Unmarshal(buf, &r)
190 if err != nil {
191 t.Fatalf("Cant' decode response. err=%s, body=%s", err, buf)
225 r, err := unmarshalResponse(buf)
226 if err != nil {
227 t.Fatalf("Can't decode response. err=%s, body=%s", err, buf)
192228 }
193229 if r.JSONRPC != jsonrpc.Version {
194230 t.Fatalf("JSONRPC Version: want=%s, got=%s", jsonrpc.Version, r.JSONRPC)