Skip to content

Commit 57e6627

Browse files
zhiqiangxufjl
andauthored
rpc: show more error detail for invalidMessageError (#30191)
Here we add distinct error messages for network timeouts and JSON parsing errors. Note this specifically applies to HTTP connections serving a single RPC request. Co-authored-by: Felix Lange <[email protected]>
1 parent ef583e9 commit 57e6627

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

rpc/server.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package rpc
1818

1919
import (
2020
"context"
21+
"errors"
2122
"io"
23+
"net"
2224
"sync"
2325
"sync/atomic"
2426

@@ -151,8 +153,8 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
151153

152154
reqs, batch, err := codec.readBatch()
153155
if err != nil {
154-
if err != io.EOF {
155-
resp := errorMessage(&invalidMessageError{"parse error"})
156+
if msg := messageForReadError(err); msg != "" {
157+
resp := errorMessage(&invalidMessageError{msg})
156158
codec.writeJSON(ctx, resp, true)
157159
}
158160
return
@@ -164,6 +166,20 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
164166
}
165167
}
166168

169+
func messageForReadError(err error) string {
170+
var netErr net.Error
171+
if errors.As(err, &netErr) {
172+
if netErr.Timeout() {
173+
return "read timeout"
174+
} else {
175+
return "read error"
176+
}
177+
} else if err != io.EOF {
178+
return "parse error"
179+
}
180+
return ""
181+
}
182+
167183
// Stop stops reading new requests, waits for stopPendingRequestTimeout to allow pending
168184
// requests to finish, then closes all codecs which will cancel pending requests and
169185
// subscriptions.

0 commit comments

Comments
 (0)