Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions transport/http/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ type requestIDKeyType struct{}

var requestIDKey requestIDKeyType

// RequestIDFromContext returns the request id from context.
func RequestIDFromContext(ctx context.Context) *RequestID {
if ctx == nil {
return nil
}

v := ctx.Value(requestIDKey)
if v == nil {
return nil
}

vv, ok := v.(*RequestID)
if !ok {
return nil
}

return vv
}

// Server wraps an endpoint and implements http.Handler.
type Server struct {
ecm EndpointCodecMap
Expand Down Expand Up @@ -198,12 +217,8 @@ func DefaultErrorEncoder(ctx context.Context, err error, w http.ResponseWriter)

w.WriteHeader(http.StatusOK)

var requestID *RequestID
if v := ctx.Value(requestIDKey); v != nil {
requestID = v.(*RequestID)
}
_ = json.NewEncoder(w).Encode(Response{
ID: requestID,
ID: RequestIDFromContext(ctx),
JSONRPC: Version,
Error: &e,
})
Expand Down