mirror of https://github.com/golang/go.git
internal/jsonrpc2*: remove usage of golang.org/x/xerrors
As of golang/go#50827, gopls no longer needs to build at Go 1.12. This was the only reason to continue using xerrors in the jsonrpc2 libraries. Remove this usage as a step toward eliminating the xerrors dependency from x/tools. For golang/go#52442 Change-Id: I1a0a1bbf57e6606c66b99b27439adf6f65c26a60 Reviewed-on: https://go-review.googlesource.com/c/tools/+/401155 Run-TryBot: Robert Findley <rfindley@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
parent
bcfc38ff69
commit
b22f048f6f
|
|
@ -6,9 +6,8 @@ package jsonrpc2
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
errors "golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Message is the interface to all jsonrpc2 message types.
|
||||
|
|
|
|||
|
|
@ -6,13 +6,13 @@ package jsonrpc2
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/tools/internal/event"
|
||||
errors "golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// NOTE: This file provides an experimental API for serving multiple remote
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ import (
|
|||
"golang.org/x/tools/internal/event"
|
||||
"golang.org/x/tools/internal/event/label"
|
||||
"golang.org/x/tools/internal/lsp/debug/tag"
|
||||
errors "golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Binder builds a connection configuration.
|
||||
|
|
@ -126,7 +125,7 @@ func newConnection(ctx context.Context, rwc io.ReadWriteCloser, binder Binder) (
|
|||
func (c *Connection) Notify(ctx context.Context, method string, params interface{}) error {
|
||||
notify, err := NewNotification(method, params)
|
||||
if err != nil {
|
||||
return errors.Errorf("marshaling notify parameters: %v", err)
|
||||
return fmt.Errorf("marshaling notify parameters: %v", err)
|
||||
}
|
||||
ctx, done := event.Start(ctx, method,
|
||||
tag.Method.Of(method),
|
||||
|
|
@ -158,7 +157,7 @@ func (c *Connection) Call(ctx context.Context, method string, params interface{}
|
|||
call, err := NewCall(result.id, method, params)
|
||||
if err != nil {
|
||||
//set the result to failed
|
||||
result.resultBox <- asyncResult{err: errors.Errorf("marshaling call parameters: %w", err)}
|
||||
result.resultBox <- asyncResult{err: fmt.Errorf("marshaling call parameters: %w", err)}
|
||||
return result
|
||||
}
|
||||
ctx, endSpan := event.Start(ctx, method,
|
||||
|
|
@ -408,7 +407,7 @@ func (c *Connection) deliverMessages(ctx context.Context, handler Handler, fromQ
|
|||
switch {
|
||||
case rerr == ErrNotHandled:
|
||||
// message not handled, report it back to the caller as an error
|
||||
c.reply(entry, nil, errors.Errorf("%w: %q", ErrMethodNotFound, entry.request.Method))
|
||||
c.reply(entry, nil, fmt.Errorf("%w: %q", ErrMethodNotFound, entry.request.Method))
|
||||
case rerr == ErrAsyncResponse:
|
||||
// message handled but the response will come later
|
||||
default:
|
||||
|
|
@ -440,7 +439,7 @@ func (c *Connection) respond(entry *incoming, result interface{}, rerr error) er
|
|||
// send the response
|
||||
if result == nil && rerr == nil {
|
||||
// call with no response, send an error anyway
|
||||
rerr = errors.Errorf("%w: %q produced no response", ErrInternal, entry.request.Method)
|
||||
rerr = fmt.Errorf("%w: %q produced no response", ErrInternal, entry.request.Method)
|
||||
}
|
||||
var response *Response
|
||||
response, err = NewResponse(entry.request.ID, result, rerr)
|
||||
|
|
@ -452,11 +451,11 @@ func (c *Connection) respond(entry *incoming, result interface{}, rerr error) er
|
|||
switch {
|
||||
case rerr != nil:
|
||||
// notification failed
|
||||
err = errors.Errorf("%w: %q notification failed: %v", ErrInternal, entry.request.Method, rerr)
|
||||
err = fmt.Errorf("%w: %q notification failed: %v", ErrInternal, entry.request.Method, rerr)
|
||||
rerr = nil
|
||||
case result != nil:
|
||||
//notification produced a response, which is an error
|
||||
err = errors.Errorf("%w: %q produced unwanted response", ErrInternal, entry.request.Method)
|
||||
err = fmt.Errorf("%w: %q produced unwanted response", ErrInternal, entry.request.Method)
|
||||
default:
|
||||
// normal notification finish
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,8 +12,6 @@ import (
|
|||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
errors "golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Reader abstracts the transport mechanics from the JSON RPC protocol.
|
||||
|
|
@ -87,7 +85,7 @@ func (w *rawWriter) Write(ctx context.Context, msg Message) (int64, error) {
|
|||
}
|
||||
data, err := EncodeMessage(msg)
|
||||
if err != nil {
|
||||
return 0, errors.Errorf("marshaling message: %v", err)
|
||||
return 0, fmt.Errorf("marshaling message: %v", err)
|
||||
}
|
||||
n, err := w.out.Write(data)
|
||||
return int64(n), err
|
||||
|
|
@ -122,7 +120,7 @@ func (r *headerReader) Read(ctx context.Context) (Message, int64, error) {
|
|||
line, err := r.in.ReadString('\n')
|
||||
total += int64(len(line))
|
||||
if err != nil {
|
||||
return nil, total, errors.Errorf("failed reading header line: %w", err)
|
||||
return nil, total, fmt.Errorf("failed reading header line: %w", err)
|
||||
}
|
||||
line = strings.TrimSpace(line)
|
||||
// check we have a header line
|
||||
|
|
@ -131,23 +129,23 @@ func (r *headerReader) Read(ctx context.Context) (Message, int64, error) {
|
|||
}
|
||||
colon := strings.IndexRune(line, ':')
|
||||
if colon < 0 {
|
||||
return nil, total, errors.Errorf("invalid header line %q", line)
|
||||
return nil, total, fmt.Errorf("invalid header line %q", line)
|
||||
}
|
||||
name, value := line[:colon], strings.TrimSpace(line[colon+1:])
|
||||
switch name {
|
||||
case "Content-Length":
|
||||
if length, err = strconv.ParseInt(value, 10, 32); err != nil {
|
||||
return nil, total, errors.Errorf("failed parsing Content-Length: %v", value)
|
||||
return nil, total, fmt.Errorf("failed parsing Content-Length: %v", value)
|
||||
}
|
||||
if length <= 0 {
|
||||
return nil, total, errors.Errorf("invalid Content-Length: %v", length)
|
||||
return nil, total, fmt.Errorf("invalid Content-Length: %v", length)
|
||||
}
|
||||
default:
|
||||
// ignoring unknown headers
|
||||
}
|
||||
}
|
||||
if length == 0 {
|
||||
return nil, total, errors.Errorf("missing Content-Length header")
|
||||
return nil, total, fmt.Errorf("missing Content-Length header")
|
||||
}
|
||||
data := make([]byte, length)
|
||||
n, err := io.ReadFull(r.in, data)
|
||||
|
|
@ -167,7 +165,7 @@ func (w *headerWriter) Write(ctx context.Context, msg Message) (int64, error) {
|
|||
}
|
||||
data, err := EncodeMessage(msg)
|
||||
if err != nil {
|
||||
return 0, errors.Errorf("marshaling message: %v", err)
|
||||
return 0, fmt.Errorf("marshaling message: %v", err)
|
||||
}
|
||||
n, err := fmt.Fprintf(w.out, "Content-Length: %v\r\n\r\n", len(data))
|
||||
total := int64(n)
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import (
|
|||
"golang.org/x/tools/internal/event/export/eventtest"
|
||||
jsonrpc2 "golang.org/x/tools/internal/jsonrpc2_v2"
|
||||
"golang.org/x/tools/internal/stack/stacktest"
|
||||
errors "golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
var callTests = []invoker{
|
||||
|
|
@ -288,19 +287,19 @@ func (h *handler) Preempt(ctx context.Context, req *jsonrpc2.Request) (interface
|
|||
case "unblock":
|
||||
var name string
|
||||
if err := json.Unmarshal(req.Params, &name); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
close(h.waiter(name))
|
||||
return nil, nil
|
||||
case "peek":
|
||||
if len(req.Params) > 0 {
|
||||
return nil, errors.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
|
||||
return nil, fmt.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
|
||||
}
|
||||
return h.accumulator, nil
|
||||
case "cancel":
|
||||
var params cancelParams
|
||||
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
h.conn.Cancel(jsonrpc2.Int64ID(params.ID))
|
||||
return nil, nil
|
||||
|
|
@ -313,50 +312,50 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{
|
|||
switch req.Method {
|
||||
case "no_args":
|
||||
if len(req.Params) > 0 {
|
||||
return nil, errors.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
|
||||
return nil, fmt.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
|
||||
}
|
||||
return true, nil
|
||||
case "one_string":
|
||||
var v string
|
||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
return "got:" + v, nil
|
||||
case "one_number":
|
||||
var v int
|
||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
return fmt.Sprintf("got:%d", v), nil
|
||||
case "set":
|
||||
var v int
|
||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
h.accumulator = v
|
||||
return nil, nil
|
||||
case "add":
|
||||
var v int
|
||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
h.accumulator += v
|
||||
return nil, nil
|
||||
case "get":
|
||||
if len(req.Params) > 0 {
|
||||
return nil, errors.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
|
||||
return nil, fmt.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
|
||||
}
|
||||
return h.accumulator, nil
|
||||
case "join":
|
||||
var v []string
|
||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
return path.Join(v...), nil
|
||||
case "echo":
|
||||
var v []interface{}
|
||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
var result interface{}
|
||||
err := h.conn.Call(ctx, v[0].(string), v[1]).Await(ctx, &result)
|
||||
|
|
@ -364,7 +363,7 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{
|
|||
case "wait":
|
||||
var name string
|
||||
if err := json.Unmarshal(req.Params, &name); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
select {
|
||||
case <-h.waiter(name):
|
||||
|
|
@ -372,12 +371,12 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{
|
|||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-time.After(time.Second):
|
||||
return nil, errors.Errorf("wait for %q timed out", name)
|
||||
return nil, fmt.Errorf("wait for %q timed out", name)
|
||||
}
|
||||
case "fork":
|
||||
var name string
|
||||
if err := json.Unmarshal(req.Params, &name); err != nil {
|
||||
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
|
||||
}
|
||||
waitFor := h.waiter(name)
|
||||
go func() {
|
||||
|
|
@ -387,7 +386,7 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{
|
|||
case <-ctx.Done():
|
||||
h.conn.Respond(req.ID, nil, ctx.Err())
|
||||
case <-time.After(time.Second):
|
||||
h.conn.Respond(req.ID, nil, errors.Errorf("wait for %q timed out", name))
|
||||
h.conn.Respond(req.ID, nil, fmt.Errorf("wait for %q timed out", name))
|
||||
}
|
||||
}()
|
||||
return nil, jsonrpc2.ErrAsyncResponse
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ package jsonrpc2
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
errors "golang.org/x/xerrors"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ID is a Request identifier.
|
||||
|
|
@ -120,7 +120,7 @@ func EncodeMessage(msg Message) ([]byte, error) {
|
|||
msg.marshal(&wire)
|
||||
data, err := json.Marshal(&wire)
|
||||
if err != nil {
|
||||
return data, errors.Errorf("marshaling jsonrpc message: %w", err)
|
||||
return data, fmt.Errorf("marshaling jsonrpc message: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
|
@ -128,10 +128,10 @@ func EncodeMessage(msg Message) ([]byte, error) {
|
|||
func DecodeMessage(data []byte) (Message, error) {
|
||||
msg := wireCombined{}
|
||||
if err := json.Unmarshal(data, &msg); err != nil {
|
||||
return nil, errors.Errorf("unmarshaling jsonrpc message: %w", err)
|
||||
return nil, fmt.Errorf("unmarshaling jsonrpc message: %w", err)
|
||||
}
|
||||
if msg.VersionTag != wireVersion {
|
||||
return nil, errors.Errorf("invalid message version tag %s expected %s", msg.VersionTag, wireVersion)
|
||||
return nil, fmt.Errorf("invalid message version tag %s expected %s", msg.VersionTag, wireVersion)
|
||||
}
|
||||
id := ID{}
|
||||
switch v := msg.ID.(type) {
|
||||
|
|
@ -144,7 +144,7 @@ func DecodeMessage(data []byte) (Message, error) {
|
|||
case string:
|
||||
id = StringID(v)
|
||||
default:
|
||||
return nil, errors.Errorf("invalid message id type <%T>%v", v, v)
|
||||
return nil, fmt.Errorf("invalid message id type <%T>%v", v, v)
|
||||
}
|
||||
if msg.Method != "" {
|
||||
// has a method, must be a call
|
||||
|
|
|
|||
|
|
@ -6,14 +6,13 @@ package jsonrpc2
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
errors "golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// Listener is implemented by protocols to accept new inbound connections.
|
||||
|
|
|
|||
Loading…
Reference in New Issue