diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 60a6c7fc..00662b05 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -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" @@ -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 {