Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonrpc.DefaultErrorEncoder: add RequestID in error body #969

Merged
merged 2 commits into from Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion transport/http/jsonrpc/server.go
Expand Up @@ -11,6 +11,8 @@ import (
httptransport "github.com/go-kit/kit/transport/http"
)

const requestIDKey = "request-id"
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved

// Server wraps an endpoint and implements http.Handler.
type Server struct {
ecm EndpointCodecMap
Expand Down Expand Up @@ -105,6 +107,8 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

ctx = context.WithValue(ctx, requestIDKey, req.ID)

// Get the endpoint and codecs from the map using the method
// defined in the JSON object
ecm, ok := s.ecm[req.Method]
Expand Down Expand Up @@ -160,7 +164,7 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// If the error implements ErrorCoder, the provided code will be set on the
// response error.
// If the error implements Headerer, the given headers will be set.
func DefaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter) {
func DefaultErrorEncoder(ctx context.Context, err error, w http.ResponseWriter) {
w.Header().Set("Content-Type", ContentType)
if headerer, ok := err.(httptransport.Headerer); ok {
for k := range headerer.Headers() {
Expand All @@ -177,7 +181,13 @@ func DefaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter) {
}

w.WriteHeader(http.StatusOK)

var requestID *RequestID
if ctx.Value(requestIDKey) != nil {
requestID = ctx.Value(requestIDKey).(*RequestID)
}
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved
_ = json.NewEncoder(w).Encode(Response{
ID: requestID,
JSONRPC: Version,
Error: &e,
})
Expand Down
34 changes: 33 additions & 1 deletion transport/http/jsonrpc/server_test.go
Expand Up @@ -24,9 +24,14 @@ func body(in string) io.Reader {
return strings.NewReader(in)
}

func expectErrorCode(t *testing.T, want int, body []byte) {
func unmarshalResponse(body []byte) (jsonrpc.Response, error) {
var r jsonrpc.Response
err := json.Unmarshal(body, &r)
return r, err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func unmarshalResponse(body []byte) (jsonrpc.Response, error) {
var r jsonrpc.Response
err := json.Unmarshal(body, &r)
return r, err
}
func unmarshalResponse(body []byte) (resp jsonrpc.Response, err error) {
return json.Unmarshal(body, &resp)
}


func expectErrorCode(t *testing.T, want int, body []byte) {
r, err := unmarshalResponse(body)
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("Cant' decode response. err=%s, body=%s", err, body)
}
Expand All @@ -38,6 +43,30 @@ func expectErrorCode(t *testing.T, want int, body []byte) {
}
}

func expectValidRequestID(t *testing.T, want int, body []byte) {
r, err := unmarshalResponse(body)
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("Cant' decode response. err=%s, body=%s", err, body)
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved
}
have, err := r.ID.Int()
if err != nil {
t.Fatalf("Cant' get requestID in response. err=%s, body=%s", err, body)
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved
}
if want != have {
t.Fatalf("Unexpected request ID. Want %d, have %d: %s", want, have, body)
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved
}
}

func expectNilRequestID(t *testing.T, body []byte) {
r, err := unmarshalResponse(body)
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("Cant' decode response. err=%s, body=%s", err, body)
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved
}
if nil != r.ID {
t.Fatalf("Unexpected request ID. Want nil, have %v", r.ID)
}
peterbourgon marked this conversation as resolved.
Show resolved Hide resolved
}

func nopDecoder(context.Context, json.RawMessage) (interface{}, error) { return struct{}{}, nil }
func nopEncoder(context.Context, interface{}) (json.RawMessage, error) { return []byte("[]"), nil }

Expand Down Expand Up @@ -92,6 +121,7 @@ func TestServerBadEndpoint(t *testing.T) {
}
buf, _ := ioutil.ReadAll(resp.Body)
expectErrorCode(t, jsonrpc.InternalError, buf)
expectValidRequestID(t, 1, buf)
}

func TestServerBadEncode(t *testing.T) {
Expand All @@ -111,6 +141,7 @@ func TestServerBadEncode(t *testing.T) {
}
buf, _ := ioutil.ReadAll(resp.Body)
expectErrorCode(t, jsonrpc.InternalError, buf)
expectValidRequestID(t, 1, buf)
}

func TestServerErrorEncoder(t *testing.T) {
Expand Down Expand Up @@ -162,6 +193,7 @@ func TestCanRejectInvalidJSON(t *testing.T) {
}
buf, _ := ioutil.ReadAll(resp.Body)
expectErrorCode(t, jsonrpc.ParseError, buf)
expectNilRequestID(t, buf)
}

func TestServerUnregisteredMethod(t *testing.T) {
Expand Down