Skip to content

Commit 2cff1af

Browse files
authored
Merge pull request nanmu42#4 from nanmu42/develop
Repair nil check of compose()
2 parents 19383e3 + 06e346a commit 2cff1af

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

account_e2e_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,27 @@ func TestClient_InternalTxByAddress(t *testing.T) {
7070
}
7171

7272
func TestClient_ERC20Transfers(t *testing.T) {
73-
const wantLen = 3
73+
const (
74+
wantLen1 = 3
75+
wantLen2 = 458
76+
)
7477

7578
var a, b = 3273004, 3328071
7679
var contract, address = "0xe0b7927c4af23765cb51314a0e0521a9645f0e2a", "0x4e83362442b8d1bec281594cea3050c8eb01311c"
7780
txs, err := api.ERC20Transfers(&contract, &address, &a, &b, 1, 500)
78-
noError(t, err, "api.ERC20Transfers")
81+
noError(t, err, "api.ERC20Transfers 1")
7982

8083
//j, _ := json.MarshalIndent(txs, "", " ")
8184
//fmt.Printf("%s\n", j)
8285

83-
if len(txs) != wantLen {
84-
t.Errorf("got txs length %v, want %v", len(txs), wantLen)
86+
if len(txs) != wantLen1 {
87+
t.Errorf("got txs length %v, want %v", len(txs), wantLen1)
88+
}
89+
90+
txs, err = api.ERC20Transfers(nil, &address, nil, &b, 1, 500)
91+
noError(t, err, "api.ERC20Transfers 2")
92+
if len(txs) != wantLen2 {
93+
t.Errorf("got txs length %v, want %v", len(txs), wantLen2)
8594
}
8695
}
8796

client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ func (c *Client) call(module, action string, param map[string]interface{}, outco
6262
defer c.AfterRequest(module, action, param, outcome, err)
6363
}
6464

65+
// recover if there shall be an panic
66+
defer func() {
67+
if r := recover(); r != nil {
68+
err = fmt.Errorf("[ouch! panic recovered] please report this with what you did and what you expected, panic detail: %v", r)
69+
}
70+
}()
71+
6572
req, err := http.NewRequest(http.MethodGet, c.craftURL(module, action, param), http.NoBody)
6673
if err != nil {
6774
err = wrapErr(err, "http.NewRequest")

helper.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,28 @@ package etherscan
99

1010
import (
1111
"math/big"
12+
"reflect"
1213
"strconv"
1314
"time"
1415
)
1516

1617
// compose adds input to param, whose key is tag
17-
// if input is nil, compose is a no-op.
18+
// if input is nil or nil of some type, compose is a no-op.
1819
func compose(param map[string]interface{}, tag string, input interface{}) {
20+
// simple situation
1921
if input == nil {
2022
return
2123
}
24+
25+
// needs dig further
26+
v := reflect.ValueOf(input)
27+
switch v.Kind() {
28+
case reflect.Ptr, reflect.Slice, reflect.Interface:
29+
if v.IsNil() {
30+
return
31+
}
32+
}
33+
2234
param[tag] = input
2335
}
2436

reflect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func valueToStr(v reflect.Value) (str string) {
4343
case reflect.Int:
4444
str = strconv.FormatInt(v.Int(), 10)
4545
default:
46-
panic(fmt.Sprintf("valueToStr: %v is of unexpected kind %q", v.Interface(), v.Kind()))
46+
panic(fmt.Sprintf("valueToStr: %v is of unexpected kind %q", v, v.Kind()))
4747
}
4848
return
4949
}

0 commit comments

Comments
 (0)