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
bc1b54354a
commit
bdd1f89abd
5 changed files with 35 additions and 40 deletions
|
|
@ -14,7 +14,6 @@ import (
|
||||||
"golang.org/x/tools/internal/event"
|
"golang.org/x/tools/internal/event"
|
||||||
"golang.org/x/tools/internal/event/label"
|
"golang.org/x/tools/internal/event/label"
|
||||||
"golang.org/x/tools/internal/lsp/debug/tag"
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
||||||
errors "golang.org/x/xerrors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Binder builds a connection configuration.
|
// 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 {
|
func (c *Connection) Notify(ctx context.Context, method string, params interface{}) error {
|
||||||
notify, err := NewNotification(method, params)
|
notify, err := NewNotification(method, params)
|
||||||
if err != nil {
|
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,
|
ctx, done := event.Start(ctx, method,
|
||||||
tag.Method.Of(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)
|
call, err := NewCall(result.id, method, params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//set the result to failed
|
//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
|
return result
|
||||||
}
|
}
|
||||||
ctx, endSpan := event.Start(ctx, method,
|
ctx, endSpan := event.Start(ctx, method,
|
||||||
|
|
@ -408,7 +407,7 @@ func (c *Connection) deliverMessages(ctx context.Context, handler Handler, fromQ
|
||||||
switch {
|
switch {
|
||||||
case rerr == ErrNotHandled:
|
case rerr == ErrNotHandled:
|
||||||
// message not handled, report it back to the caller as an error
|
// 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:
|
case rerr == ErrAsyncResponse:
|
||||||
// message handled but the response will come later
|
// message handled but the response will come later
|
||||||
default:
|
default:
|
||||||
|
|
@ -440,7 +439,7 @@ func (c *Connection) respond(entry *incoming, result interface{}, rerr error) er
|
||||||
// send the response
|
// send the response
|
||||||
if result == nil && rerr == nil {
|
if result == nil && rerr == nil {
|
||||||
// call with no response, send an error anyway
|
// 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
|
var response *Response
|
||||||
response, err = NewResponse(entry.request.ID, result, rerr)
|
response, err = NewResponse(entry.request.ID, result, rerr)
|
||||||
|
|
@ -452,11 +451,11 @@ func (c *Connection) respond(entry *incoming, result interface{}, rerr error) er
|
||||||
switch {
|
switch {
|
||||||
case rerr != nil:
|
case rerr != nil:
|
||||||
// notification failed
|
// 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
|
rerr = nil
|
||||||
case result != nil:
|
case result != nil:
|
||||||
//notification produced a response, which is an error
|
//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:
|
default:
|
||||||
// normal notification finish
|
// normal notification finish
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
errors "golang.org/x/xerrors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Reader abstracts the transport mechanics from the JSON RPC protocol.
|
// 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)
|
data, err := EncodeMessage(msg)
|
||||||
if err != nil {
|
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)
|
n, err := w.out.Write(data)
|
||||||
return int64(n), err
|
return int64(n), err
|
||||||
|
|
@ -122,7 +120,7 @@ func (r *headerReader) Read(ctx context.Context) (Message, int64, error) {
|
||||||
line, err := r.in.ReadString('\n')
|
line, err := r.in.ReadString('\n')
|
||||||
total += int64(len(line))
|
total += int64(len(line))
|
||||||
if err != nil {
|
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)
|
line = strings.TrimSpace(line)
|
||||||
// check we have a header 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, ':')
|
colon := strings.IndexRune(line, ':')
|
||||||
if colon < 0 {
|
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:])
|
name, value := line[:colon], strings.TrimSpace(line[colon+1:])
|
||||||
switch name {
|
switch name {
|
||||||
case "Content-Length":
|
case "Content-Length":
|
||||||
if length, err = strconv.ParseInt(value, 10, 32); err != nil {
|
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 {
|
if length <= 0 {
|
||||||
return nil, total, errors.Errorf("invalid Content-Length: %v", length)
|
return nil, total, fmt.Errorf("invalid Content-Length: %v", length)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
// ignoring unknown headers
|
// ignoring unknown headers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if length == 0 {
|
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)
|
data := make([]byte, length)
|
||||||
n, err := io.ReadFull(r.in, data)
|
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)
|
data, err := EncodeMessage(msg)
|
||||||
if err != nil {
|
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))
|
n, err := fmt.Fprintf(w.out, "Content-Length: %v\r\n\r\n", len(data))
|
||||||
total := int64(n)
|
total := int64(n)
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ import (
|
||||||
"golang.org/x/tools/internal/event/export/eventtest"
|
"golang.org/x/tools/internal/event/export/eventtest"
|
||||||
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
|
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
|
||||||
"golang.org/x/tools/internal/stack/stacktest"
|
"golang.org/x/tools/internal/stack/stacktest"
|
||||||
errors "golang.org/x/xerrors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var callTests = []invoker{
|
var callTests = []invoker{
|
||||||
|
|
@ -288,19 +287,19 @@ func (h *handler) Preempt(ctx context.Context, req *jsonrpc2.Request) (interface
|
||||||
case "unblock":
|
case "unblock":
|
||||||
var name string
|
var name string
|
||||||
if err := json.Unmarshal(req.Params, &name); err != nil {
|
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))
|
close(h.waiter(name))
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case "peek":
|
case "peek":
|
||||||
if len(req.Params) > 0 {
|
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
|
return h.accumulator, nil
|
||||||
case "cancel":
|
case "cancel":
|
||||||
var params cancelParams
|
var params cancelParams
|
||||||
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
|
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))
|
h.conn.Cancel(jsonrpc2.Int64ID(params.ID))
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
@ -313,50 +312,50 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{
|
||||||
switch req.Method {
|
switch req.Method {
|
||||||
case "no_args":
|
case "no_args":
|
||||||
if len(req.Params) > 0 {
|
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
|
return true, nil
|
||||||
case "one_string":
|
case "one_string":
|
||||||
var v string
|
var v string
|
||||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
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
|
return "got:" + v, nil
|
||||||
case "one_number":
|
case "one_number":
|
||||||
var v int
|
var v int
|
||||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
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
|
return fmt.Sprintf("got:%d", v), nil
|
||||||
case "set":
|
case "set":
|
||||||
var v int
|
var v int
|
||||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
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
|
h.accumulator = v
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case "add":
|
case "add":
|
||||||
var v int
|
var v int
|
||||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
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
|
h.accumulator += v
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case "get":
|
case "get":
|
||||||
if len(req.Params) > 0 {
|
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
|
return h.accumulator, nil
|
||||||
case "join":
|
case "join":
|
||||||
var v []string
|
var v []string
|
||||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
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
|
return path.Join(v...), nil
|
||||||
case "echo":
|
case "echo":
|
||||||
var v []interface{}
|
var v []interface{}
|
||||||
if err := json.Unmarshal(req.Params, &v); err != nil {
|
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{}
|
var result interface{}
|
||||||
err := h.conn.Call(ctx, v[0].(string), v[1]).Await(ctx, &result)
|
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":
|
case "wait":
|
||||||
var name string
|
var name string
|
||||||
if err := json.Unmarshal(req.Params, &name); err != nil {
|
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 {
|
select {
|
||||||
case <-h.waiter(name):
|
case <-h.waiter(name):
|
||||||
|
|
@ -372,12 +371,12 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil, ctx.Err()
|
return nil, ctx.Err()
|
||||||
case <-time.After(time.Second):
|
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":
|
case "fork":
|
||||||
var name string
|
var name string
|
||||||
if err := json.Unmarshal(req.Params, &name); err != nil {
|
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)
|
waitFor := h.waiter(name)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -387,7 +386,7 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
h.conn.Respond(req.ID, nil, ctx.Err())
|
h.conn.Respond(req.ID, nil, ctx.Err())
|
||||||
case <-time.After(time.Second):
|
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
|
return nil, jsonrpc2.ErrAsyncResponse
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ package jsonrpc2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
errors "golang.org/x/xerrors"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ID is a Request identifier.
|
// ID is a Request identifier.
|
||||||
|
|
@ -120,7 +120,7 @@ func EncodeMessage(msg Message) ([]byte, error) {
|
||||||
msg.marshal(&wire)
|
msg.marshal(&wire)
|
||||||
data, err := json.Marshal(&wire)
|
data, err := json.Marshal(&wire)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return data, errors.Errorf("marshaling jsonrpc message: %w", err)
|
return data, fmt.Errorf("marshaling jsonrpc message: %w", err)
|
||||||
}
|
}
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
@ -128,10 +128,10 @@ func EncodeMessage(msg Message) ([]byte, error) {
|
||||||
func DecodeMessage(data []byte) (Message, error) {
|
func DecodeMessage(data []byte) (Message, error) {
|
||||||
msg := wireCombined{}
|
msg := wireCombined{}
|
||||||
if err := json.Unmarshal(data, &msg); err != nil {
|
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 {
|
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{}
|
id := ID{}
|
||||||
switch v := msg.ID.(type) {
|
switch v := msg.ID.(type) {
|
||||||
|
|
@ -144,7 +144,7 @@ func DecodeMessage(data []byte) (Message, error) {
|
||||||
case string:
|
case string:
|
||||||
id = StringID(v)
|
id = StringID(v)
|
||||||
default:
|
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 != "" {
|
if msg.Method != "" {
|
||||||
// has a method, must be a call
|
// has a method, must be a call
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,13 @@ package jsonrpc2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
errors "golang.org/x/xerrors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Listener is implemented by protocols to accept new inbound connections.
|
// Listener is implemented by protocols to accept new inbound connections.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue