Skip to content

Commit

Permalink
jsonrpc error check (#136)
Browse files Browse the repository at this point in the history
* jsonrpc error check

* add ellipsis and length

* rename vars
  • Loading branch information
klaidliadon committed Sep 3, 2024
1 parent 229891a commit d41fe41
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ethrpc/ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"sync/atomic"

"github.com/0xsequence/ethkit/ethrpc/jsonrpc"
"github.com/0xsequence/ethkit/go-ethereum"
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind"
"github.com/0xsequence/ethkit/go-ethereum/common"
Expand Down Expand Up @@ -109,14 +110,19 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) {

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, superr.Wrap(ErrRequestFail, fmt.Errorf("failed to read resposne body: %w", err))
return nil, superr.Wrap(ErrRequestFail, fmt.Errorf("failed to read response body: %w", err))
}

if (res.StatusCode < 200 || res.StatusCode > 299) && res.StatusCode != 401 {
msg := jsonrpc.Message{}
if err := json.Unmarshal(body, &msg); err == nil && msg.Error != nil {
return body, superr.Wrap(ErrRequestFail, msg.Error)
}
details := any(body)
if len(body) > 100 {
body = body[:100]
details = fmt.Sprintf("%s … (%d bytes)", body[:100], len(body))
}
return body, superr.Wrap(ErrRequestFail, fmt.Errorf("non-200 response with status code: %d with body '%s'", res.StatusCode, body))
return body, superr.Wrap(ErrRequestFail, fmt.Errorf("non-200 response with status code: %d with body '%s'", res.StatusCode, details))
}

if err := json.Unmarshal(body, &batch); err != nil {
Expand Down

0 comments on commit d41fe41

Please sign in to comment.