Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonrpc error check #136

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines +118 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!!

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
Loading