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.
This commit is contained in:
Owen
2019-04-24 11:36:47 -04:00
parent 46a2f52ca6
commit 92d56725f7
2 changed files with 11 additions and 4 deletions

View File

@@ -95,7 +95,7 @@ func (c *Client) Verify(ctx context.Context, userID string, receiptID string) (I
resp, err := c.httpCli.Do(req)
if err != nil {
return result, fmt.Errorf("%v", err)
return result, err
}
defer resp.Body.Close()

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"reflect"
"testing"
@@ -143,11 +144,17 @@ func TestVerifyTimeout(t *testing.T) {
server, client := testTools(100, "timeout response")
defer server.Close()
expected := errors.New("")
ctx := context.Background()
_, actual := client.Verify(ctx, "timeout", "timeout")
if !reflect.DeepEqual(reflect.TypeOf(actual), reflect.TypeOf(expected)) {
t.Errorf("got %v\nwant %v", actual, expected)
// Actual should be a "request canceled" *url.Error
urlErr, ok := actual.(*url.Error)
if !ok {
t.Errorf("Expected *url.Error, got %T", actual)
}
if !urlErr.Timeout() {
t.Errorf("got %v\nwant timeout", actual)
}
}