Skip to content

Commit

Permalink
fixed output of CallErrors.Error
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Apr 27, 2023
1 parent fee24f8 commit 9e908da
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 7 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (c *Client) Call(calls ...w3types.Caller) error {
type CallErrors []error

func (e CallErrors) Error() string {
if len(e) == 1 {
if len(e) == 1 && e[0] != nil {
return fmt.Sprintf("w3: call failed: %s", e[0])
}

Expand All @@ -137,7 +137,12 @@ func (e CallErrors) Error() string {
}
errors = append(errors, fmt.Sprintf("call[%d]: %s", i, err))
}
return fmt.Sprintf("w3: %d calls failed:\n%s", len(errors), strings.Join(errors, "\n"))

var plr string
if len(errors) > 1 {
plr = "s"
}
return fmt.Sprintf("w3: %d call%s failed:\n%s", len(errors), plr, strings.Join(errors, "\n"))
}

func (e CallErrors) Is(target error) bool {
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ func TestClientCall(t *testing.T) {
&testCaller{ReturnErr: errors.New("err")},
&testCaller{},
},
WantErr: errors.New("w3: one ore more calls failed"),
WantErr: errors.New("w3: 1 call failed:\ncall[0]: err"),
},
{
Buf: bytes.NewBufferString(jsonCalls2),
Calls: []w3types.Caller{
&testCaller{},
&testCaller{ReturnErr: errors.New("err")},
},
WantErr: errors.New("w3: one ore more calls failed"),
WantErr: errors.New("w3: 1 call failed:\ncall[1]: err"),
},
{
Buf: bytes.NewBufferString(jsonCalls2),
Calls: []w3types.Caller{
&testCaller{ReturnErr: errors.New("err")},
&testCaller{ReturnErr: errors.New("err")},
},
WantErr: errors.New("w3: one ore more calls failed"),
WantErr: errors.New("w3: 2 calls failed:\ncall[0]: err\ncall[1]: err"),
},
}

Expand Down

0 comments on commit 9e908da

Please sign in to comment.