From fac141e8496c85da4e3c40d6a8b6a3c090c19e48 Mon Sep 17 00:00:00 2001 From: Alex Guerrieri Date: Tue, 3 Sep 2024 13:40:00 +0200 Subject: [PATCH 1/3] jsonrpc error check --- ethrpc/ethrpc.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 60a6c7fc..560b70ec 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" @@ -113,6 +114,10 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) { } 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) + } if len(body) > 100 { body = body[:100] } From ed5aa47e62523d5dfbfe7b0fdac920c3057b45d4 Mon Sep 17 00:00:00 2001 From: Alex Guerrieri Date: Tue, 3 Sep 2024 18:29:52 +0200 Subject: [PATCH 2/3] add ellipsis and length --- ethrpc/ethrpc.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 560b70ec..897ab800 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -108,27 +108,28 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) { } defer res.Body.Close() - body, err := io.ReadAll(res.Body) + raw, 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) + if err := json.Unmarshal(raw, &msg); err == nil && msg.Error != nil { + return raw, superr.Wrap(ErrRequestFail, msg.Error) } - if len(body) > 100 { - body = body[:100] + body := any(raw) + if len(raw) > 100 { + body = fmt.Sprintf("%s … (%d bytes)", raw[:100], len(raw)) } - return body, superr.Wrap(ErrRequestFail, fmt.Errorf("non-200 response with status code: %d with body '%s'", res.StatusCode, body)) + return raw, superr.Wrap(ErrRequestFail, fmt.Errorf("non-200 response with status code: %d with body '%s'", res.StatusCode, body)) } - if err := json.Unmarshal(body, &batch); err != nil { - if len(body) > 100 { - body = body[:100] + if err := json.Unmarshal(raw, &batch); err != nil { + if len(raw) > 100 { + raw = raw[:100] } - return body, superr.Wrap(ErrRequestFail, fmt.Errorf("failed to unmarshal response: '%s' due to %w", string(body), err)) + return raw, superr.Wrap(ErrRequestFail, fmt.Errorf("failed to unmarshal response: '%s' due to %w", string(raw), err)) } for i, call := range batch { @@ -160,7 +161,7 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) { } } - return body, batch.ErrorOrNil() + return raw, batch.ErrorOrNil() } func (p *Provider) ChainID(ctx context.Context) (*big.Int, error) { From e52049f0007d0c86d0e9cffcd9d9be3872d3c1fc Mon Sep 17 00:00:00 2001 From: Alex Guerrieri Date: Tue, 3 Sep 2024 18:31:24 +0200 Subject: [PATCH 3/3] rename vars --- ethrpc/ethrpc.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 897ab800..00662b05 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -108,28 +108,28 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) { } defer res.Body.Close() - raw, err := io.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { 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(raw, &msg); err == nil && msg.Error != nil { - return raw, superr.Wrap(ErrRequestFail, msg.Error) + if err := json.Unmarshal(body, &msg); err == nil && msg.Error != nil { + return body, superr.Wrap(ErrRequestFail, msg.Error) } - body := any(raw) - if len(raw) > 100 { - body = fmt.Sprintf("%s … (%d bytes)", raw[:100], len(raw)) + details := any(body) + if len(body) > 100 { + details = fmt.Sprintf("%s … (%d bytes)", body[:100], len(body)) } - return raw, 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(raw, &batch); err != nil { - if len(raw) > 100 { - raw = raw[:100] + if err := json.Unmarshal(body, &batch); err != nil { + if len(body) > 100 { + body = body[:100] } - return raw, superr.Wrap(ErrRequestFail, fmt.Errorf("failed to unmarshal response: '%s' due to %w", string(raw), err)) + return body, superr.Wrap(ErrRequestFail, fmt.Errorf("failed to unmarshal response: '%s' due to %w", string(body), err)) } for i, call := range batch { @@ -161,7 +161,7 @@ func (p *Provider) Do(ctx context.Context, calls ...Call) ([]byte, error) { } } - return raw, batch.ErrorOrNil() + return body, batch.ErrorOrNil() } func (p *Provider) ChainID(ctx context.Context) (*big.Int, error) {