Skip to content

Commit 0ec5ab4

Browse files
fjlkaralabe
authored andcommitted
common: improve GraphQL error messages (#20354)
1 parent 0754100 commit 0ec5ab4

3 files changed

Lines changed: 32 additions & 54 deletions

File tree

‎common/bytes_test.go‎

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,43 @@ package common
1919
import (
2020
"bytes"
2121
"testing"
22-
23-
checker "gopkg.in/check.v1"
2422
)
2523

26-
type BytesSuite struct{}
27-
28-
var _ = checker.Suite(&BytesSuite{})
24+
func TestCopyBytes(t *testing.T) {
25+
input := []byte{1, 2, 3, 4}
2926

30-
func (s *BytesSuite) TestCopyBytes(c *checker.C) {
31-
data1 := []byte{1, 2, 3, 4}
32-
exp1 := []byte{1, 2, 3, 4}
33-
res1 := CopyBytes(data1)
34-
c.Assert(res1, checker.DeepEquals, exp1)
27+
v := CopyBytes(input)
28+
if !bytes.Equal(v, []byte{1, 2, 3, 4}) {
29+
t.Fatal("not equal after copy")
30+
}
31+
v[0] = 99
32+
if bytes.Equal(v, input) {
33+
t.Fatal("result is not a copy")
34+
}
3535
}
3636

37-
func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
38-
val1 := []byte{1, 2, 3, 4}
39-
exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
40-
41-
res1 := LeftPadBytes(val1, 8)
42-
res2 := LeftPadBytes(val1, 2)
37+
func TestLeftPadBytes(t *testing.T) {
38+
val := []byte{1, 2, 3, 4}
39+
padded := []byte{0, 0, 0, 0, 1, 2, 3, 4}
4340

44-
c.Assert(res1, checker.DeepEquals, exp1)
45-
c.Assert(res2, checker.DeepEquals, val1)
41+
if r := LeftPadBytes(val, 8); !bytes.Equal(r, padded) {
42+
t.Fatalf("LeftPadBytes(%v, 8) == %v", val, r)
43+
}
44+
if r := LeftPadBytes(val, 2); !bytes.Equal(r, val) {
45+
t.Fatalf("LeftPadBytes(%v, 2) == %v", val, r)
46+
}
4647
}
4748

48-
func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
49+
func TestRightPadBytes(t *testing.T) {
4950
val := []byte{1, 2, 3, 4}
50-
exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
51-
52-
resstd := RightPadBytes(val, 8)
53-
resshrt := RightPadBytes(val, 2)
51+
padded := []byte{1, 2, 3, 4, 0, 0, 0, 0}
5452

55-
c.Assert(resstd, checker.DeepEquals, exp)
56-
c.Assert(resshrt, checker.DeepEquals, val)
53+
if r := RightPadBytes(val, 8); !bytes.Equal(r, padded) {
54+
t.Fatalf("RightPadBytes(%v, 8) == %v", val, r)
55+
}
56+
if r := RightPadBytes(val, 2); !bytes.Equal(r, val) {
57+
t.Fatalf("RightPadBytes(%v, 2) == %v", val, r)
58+
}
5759
}
5860

5961
func TestFromHex(t *testing.T) {

‎common/main_test.go‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎common/types.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"database/sql/driver"
2121
"encoding/hex"
2222
"encoding/json"
23+
"errors"
2324
"fmt"
2425
"math/big"
2526
"math/rand"
@@ -142,7 +143,7 @@ func (h Hash) Value() (driver.Value, error) {
142143
}
143144

144145
// ImplementsGraphQLType returns true if Hash implements the specified GraphQL type.
145-
func (_ Hash) ImplementsGraphQLType(name string) bool { return name == "Bytes32" }
146+
func (Hash) ImplementsGraphQLType(name string) bool { return name == "Bytes32" }
146147

147148
// UnmarshalGraphQL unmarshals the provided GraphQL query data.
148149
func (h *Hash) UnmarshalGraphQL(input interface{}) error {
@@ -151,7 +152,7 @@ func (h *Hash) UnmarshalGraphQL(input interface{}) error {
151152
case string:
152153
err = h.UnmarshalText([]byte(input))
153154
default:
154-
err = fmt.Errorf("Unexpected type for Bytes32: %v", input)
155+
err = fmt.Errorf("unexpected type %T for Hash", input)
155156
}
156157
return err
157158
}
@@ -290,7 +291,7 @@ func (a *Address) UnmarshalGraphQL(input interface{}) error {
290291
case string:
291292
err = a.UnmarshalText([]byte(input))
292293
default:
293-
err = fmt.Errorf("Unexpected type for Address: %v", input)
294+
err = fmt.Errorf("unexpected type %T for Address", input)
294295
}
295296
return err
296297
}
@@ -323,7 +324,7 @@ func NewMixedcaseAddress(addr Address) MixedcaseAddress {
323324
// NewMixedcaseAddressFromString is mainly meant for unit-testing
324325
func NewMixedcaseAddressFromString(hexaddr string) (*MixedcaseAddress, error) {
325326
if !IsHexAddress(hexaddr) {
326-
return nil, fmt.Errorf("Invalid address")
327+
return nil, errors.New("invalid address")
327328
}
328329
a := FromHex(hexaddr)
329330
return &MixedcaseAddress{addr: BytesToAddress(a), original: hexaddr}, nil

0 commit comments

Comments
 (0)