Skip to content

Commit 92d5672

Browse files
committed
Unwrap error in Amazon validator
In amazon/validator.go, an error was being wrapped by fmt.Errorf("%v", err). That discards any type information about the returned error; returning err lets the callers inspect the exact error returned by the HTTP client. The test had to change to do similar inspection.
1 parent 46a2f52 commit 92d5672

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

amazon/validator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (c *Client) Verify(ctx context.Context, userID string, receiptID string) (I
9595

9696
resp, err := c.httpCli.Do(req)
9797
if err != nil {
98-
return result, fmt.Errorf("%v", err)
98+
return result, err
9999
}
100100
defer resp.Body.Close()
101101

amazon/validator_test.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"net/http/httptest"
9+
"net/url"
910
"os"
1011
"reflect"
1112
"testing"
@@ -143,11 +144,17 @@ func TestVerifyTimeout(t *testing.T) {
143144
server, client := testTools(100, "timeout response")
144145
defer server.Close()
145146

146-
expected := errors.New("")
147147
ctx := context.Background()
148148
_, actual := client.Verify(ctx, "timeout", "timeout")
149-
if !reflect.DeepEqual(reflect.TypeOf(actual), reflect.TypeOf(expected)) {
150-
t.Errorf("got %v\nwant %v", actual, expected)
149+
150+
// Actual should be a "request canceled" *url.Error
151+
urlErr, ok := actual.(*url.Error)
152+
if !ok {
153+
t.Errorf("Expected *url.Error, got %T", actual)
154+
}
155+
156+
if !urlErr.Timeout() {
157+
t.Errorf("got %v\nwant timeout", actual)
151158
}
152159
}
153160

0 commit comments

Comments
 (0)