Skip to content

Commit ef1b43a

Browse files
committed
Do not treat server errors as API originated errors
1 parent 39e5821 commit ef1b43a

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

extras/lbryinc/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ func (c Client) doCall(url string, payload string) ([]byte, error) {
119119
if err != nil {
120120
return body, err
121121
}
122+
if r.StatusCode >= 500 {
123+
return body, fmt.Errorf("server returned non-OK status: %v", r.StatusCode)
124+
}
122125
defer r.Body.Close()
123126
return ioutil.ReadAll(r.Body)
124127
}

extras/lbryinc/client_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12-
func launchDummyServer(lastReq **http.Request, path, response string) *httptest.Server {
12+
func launchDummyServer(lastReq **http.Request, path, response string, status int) *httptest.Server {
1313
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14-
*lastReq = &*r
14+
if lastReq != nil {
15+
*lastReq = &*r
16+
}
1517
if r.URL.Path != path {
1618
fmt.Printf("path doesn't match: %v != %v", r.URL.Path, path)
1719
w.WriteHeader(http.StatusNotFound)
1820
} else {
1921
w.Header().Set("Content-Type", "application/json; charset=utf-8")
20-
w.WriteHeader(http.StatusOK)
22+
w.WriteHeader(status)
2123
w.Write([]byte(response))
2224
}
2325
}))
2426
}
2527

2628
func TestUserMe(t *testing.T) {
27-
var req *http.Request
28-
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
29+
ts := launchDummyServer(nil, makeMethodPath(userObjectPath, userMeMethod), userMeResponse, http.StatusOK)
2930
defer ts.Close()
3031

3132
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
@@ -35,8 +36,7 @@ func TestUserMe(t *testing.T) {
3536
}
3637

3738
func TestUserHasVerifiedEmail(t *testing.T) {
38-
var req *http.Request
39-
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse)
39+
ts := launchDummyServer(nil, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse, http.StatusOK)
4040
defer ts.Close()
4141

4242
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
@@ -48,7 +48,7 @@ func TestUserHasVerifiedEmail(t *testing.T) {
4848

4949
func TestRemoteIP(t *testing.T) {
5050
var req *http.Request
51-
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
51+
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse, http.StatusOK)
5252
defer ts.Close()
5353

5454
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL, RemoteIP: "8.8.8.8"})
@@ -74,6 +74,17 @@ func TestHTTPError(t *testing.T) {
7474
assert.EqualError(t, err, `Post "http://lolcathost/user/has_verified_email": dial tcp: lookup lolcathost: no such host`)
7575
}
7676

77+
func TestGatewayError(t *testing.T) {
78+
var req *http.Request
79+
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), "", http.StatusBadGateway)
80+
defer ts.Close()
81+
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: ts.URL})
82+
83+
r, err := c.UserHasVerifiedEmail()
84+
assert.Nil(t, r)
85+
assert.EqualError(t, err, `server returned non-OK status: 502`)
86+
}
87+
7788
const userMeResponse = `{
7889
"success": true,
7990
"error": null,

0 commit comments

Comments
 (0)