mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
rpc: add specific messages for network errors
This commit is contained in:
parent
204e00a91f
commit
dc3ca14f8a
1 changed files with 18 additions and 3 deletions
|
|
@ -18,8 +18,9 @@ package rpc
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
|
|
@ -152,8 +153,8 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
|
|||
|
||||
reqs, batch, err := codec.readBatch()
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
resp := errorMessage(&invalidMessageError{fmt.Sprintf("parse error: %s", err.Error())})
|
||||
if msg := messageForReadError(err); msg != "" {
|
||||
resp := errorMessage(&invalidMessageError{msg})
|
||||
codec.writeJSON(ctx, resp, true)
|
||||
}
|
||||
return
|
||||
|
|
@ -165,6 +166,20 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
|
|||
}
|
||||
}
|
||||
|
||||
func messageForReadError(err error) string {
|
||||
var netErr net.Error
|
||||
if errors.As(err, &netErr) {
|
||||
if netErr.Timeout() {
|
||||
return "read timeout"
|
||||
} else {
|
||||
return "read error"
|
||||
}
|
||||
} else if err != io.EOF {
|
||||
return "parse error"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Stop stops reading new requests, waits for stopPendingRequestTimeout to allow pending
|
||||
// requests to finish, then closes all codecs which will cancel pending requests and
|
||||
// subscriptions.
|
||||
|
|
|
|||
Loading…
Reference in a new issue