Skip to content

Commit fff0d85

Browse files
committed
all: fix lint suggestions
1 parent a64f450 commit fff0d85

File tree

12 files changed

+34
-27
lines changed

12 files changed

+34
-27
lines changed

conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func NewConn(s Stream) Conn {
7777
func (c *conn) Notify(ctx context.Context, method string, params interface{}) (err error) {
7878
notify, err := NewNotification(method, params)
7979
if err != nil {
80-
return fmt.Errorf("marshaling notify parameters: %v", err)
80+
return fmt.Errorf("marshaling notify parameters: %w", err)
8181
}
8282

8383
_, err = c.write(ctx, notify)
@@ -113,7 +113,7 @@ func (c *conn) write(ctx context.Context, msg Message) (n int64, err error) {
113113
c.writeMu.Lock()
114114
n, err = c.stream.Write(ctx, msg)
115115
c.writeMu.Unlock()
116-
return n, err
116+
return
117117
}
118118

119119
// Go implemens Conn.

conn_json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (c *conn) Call(ctx context.Context, method string, params, result interface
2020
id := ID{number: atomic.AddInt64(&c.seq, 1)}
2121
call, err := NewRequest(id, method, params)
2222
if err != nil {
23-
return id, fmt.Errorf("marshaling call parameters: %v", err)
23+
return id, fmt.Errorf("marshaling call parameters: %w", err)
2424
}
2525

2626
// We have to add ourselves to the pending map before we send, otherwise we
@@ -59,7 +59,7 @@ func (c *conn) Call(ctx context.Context, method string, params, result interface
5959
default:
6060
dec := json.NewDecoder(bytes.NewReader(response.result))
6161
if err := dec.Decode(result); err != nil {
62-
return id, fmt.Errorf("unmarshaling result: %v", err)
62+
return id, fmt.Errorf("unmarshaling result: %w", err)
6363
}
6464
return id, nil
6565
}

fake/fake_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func fakeHandler(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Reque
2121
}
2222

2323
func TestTestServer(t *testing.T) {
24-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
24+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
2525
defer cancel()
2626

2727
server := jsonrpc2.HandlerServer(fakeHandler)

handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func AsyncHandler(handler Handler) Handler {
100100
}
101101
go func() {
102102
<-waitForPrevious
103-
handler(ctx, reply, req) //nolint:errcheck
103+
handler(ctx, reply, req)
104104
}()
105105
return nil
106106
}

jsonrpc2_json_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func (test *callTest) newResults() interface{} {
4848
}
4949

5050
func (test *callTest) verifyResults(t *testing.T, results interface{}) {
51+
t.Helper()
52+
5153
if results == nil {
5254
return
5355
}
@@ -78,6 +80,8 @@ func TestRequest(t *testing.T) {
7880
}
7981

8082
func prepare(ctx context.Context, t *testing.T) (conn, conn2 jsonrpc2.Conn, done func()) {
83+
t.Helper()
84+
8185
// make a wait group that can be used to wait for the system to shut down
8286
aPipe, bPipe := net.Pipe()
8387
a := run(ctx, aPipe)
@@ -108,19 +112,19 @@ func testHandler() jsonrpc2.Handler {
108112
case "one_string":
109113
var v string
110114
if err := json.Unmarshal(req.Params(), &v); err != nil {
111-
return reply(ctx, nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err))
115+
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
112116
}
113117
return reply(ctx, "got:"+v, nil)
114118
case "one_number":
115119
var v int
116120
if err := json.Unmarshal(req.Params(), &v); err != nil {
117-
return reply(ctx, nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err))
121+
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
118122
}
119123
return reply(ctx, fmt.Sprintf("got:%d", v), nil)
120124
case "join":
121125
var v []string
122126
if err := json.Unmarshal(req.Params(), &v); err != nil {
123-
return reply(ctx, nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err))
127+
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
124128
}
125129
return reply(ctx, path.Join(v...), nil)
126130
default:

message.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ func toError(err error) *Error {
179179
// no error, the response is complete
180180
return nil
181181
}
182-
if err, ok := err.(*Error); ok {
182+
var wrapped *Error
183+
if errors.As(err, &wrapped) {
183184
// already a wire error, just use it
184-
return err
185+
return wrapped
185186
}
186187
result := &Error{Message: err.Error()}
187-
var wrapped *Error
188188
if errors.As(err, &wrapped) {
189189
// if we wrapped a wire error, keep the code from the wrapped error
190190
// but the message from the outer error

message_json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func marshalInterface(obj interface{}) (RawMessage, error) {
1616
data, err := json.Marshal(obj)
1717
if err != nil {
18-
return RawMessage{}, err
18+
return RawMessage{}, fmt.Errorf("failed to marshal json: %w", err)
1919
}
2020
return RawMessage(data), nil
2121
}

serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func HandlerServer(h Handler) StreamServer {
4949
func ListenAndServe(ctx context.Context, network, addr string, server StreamServer, idleTimeout time.Duration) error {
5050
ln, err := net.Listen(network, addr)
5151
if err != nil {
52-
return err
52+
return fmt.Errorf("failed to listen %s:%s: %w", network, addr, err)
5353
}
5454
defer ln.Close()
5555
if network == "unix" {

serve_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package jsonrpc2
55

66
import (
77
"context"
8+
"errors"
89
"net"
910
"sync"
1011
"testing"
@@ -52,7 +53,7 @@ func TestIdleTimeout(t *testing.T) {
5253

5354
wg.Wait()
5455

55-
if runErr != ErrIdleTimeout {
56+
if !errors.Is(runErr, ErrIdleTimeout) {
5657
t.Errorf("run() returned error %v, want %v", runErr, ErrIdleTimeout)
5758
}
5859
}

stream.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ func (s *rawStream) Read(ctx context.Context) (Message, int64, error) {
4242
}
4343
var raw RawMessage
4444
if err := s.in.Decode(&raw); err != nil {
45-
return nil, 0, err
45+
return nil, 0, fmt.Errorf("failed to Decode: %w", err)
4646
}
4747
msg, err := DecodeMessage(raw)
48-
return msg, int64(len(raw)), err
48+
return msg, int64(len(raw)), fmt.Errorf("failed to DecodeMessage: %w", err)
4949
}
5050

5151
func (s *rawStream) Close() error {
@@ -108,7 +108,7 @@ func (s *headerStream) Read(ctx context.Context) (Message, int64, error) {
108108
}
109109
data := make([]byte, length)
110110
if _, err := io.ReadFull(s.in, data); err != nil {
111-
return nil, total, err
111+
return nil, total, fmt.Errorf("failed to ReadFull: %w", err)
112112
}
113113
total += length
114114
msg, err := DecodeMessage(data)

0 commit comments

Comments
 (0)