Skip to content

Commit 06f92ef

Browse files
authored
Merge pull request #19 from erutherford/8-check-status-code
8 - Provide clearer error for non-200 status codes without JSON
2 parents 05b17f3 + ccab8c5 commit 06f92ef

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ go:
44
- 1.7.x
55
- 1.8.x
66
- 1.9.x
7+
- 1.10.x
78

89
before_install:
9-
- go get github.com/golang/lint/golint
10+
- go get golang.org/x/lint/golint
1011

1112
before_script:
1213
- go vet ./...

graphql.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func (c *Client) runWithJSON(ctx context.Context, req *Request, resp interface{}
134134
}
135135
c.logf("<< %s", buf.String())
136136
if err := json.NewDecoder(&buf).Decode(&gr); err != nil {
137+
if res.StatusCode != http.StatusOK {
138+
return fmt.Errorf("graphql: server returned a non-200 status code: %v", res.StatusCode)
139+
}
137140
return errors.Wrap(err, "decoding response")
138141
}
139142
if len(gr.Errors) > 0 {
@@ -201,6 +204,9 @@ func (c *Client) runWithPostFields(ctx context.Context, req *Request, resp inter
201204
}
202205
c.logf("<< %s", buf.String())
203206
if err := json.NewDecoder(&buf).Decode(&gr); err != nil {
207+
if res.StatusCode != http.StatusOK {
208+
return fmt.Errorf("graphql: server returned a non-200 status code: %v", res.StatusCode)
209+
}
204210
return errors.Wrap(err, "decoding response")
205211
}
206212
if len(gr.Errors) > 0 {

graphql_json_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,60 @@ func TestDoJSON(t *testing.T) {
4141
is.Equal(responseData["something"], "yes")
4242
}
4343

44+
func TestDoJSONServerError(t *testing.T) {
45+
is := is.New(t)
46+
var calls int
47+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48+
calls++
49+
is.Equal(r.Method, http.MethodPost)
50+
b, err := ioutil.ReadAll(r.Body)
51+
is.NoErr(err)
52+
is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
53+
w.WriteHeader(http.StatusInternalServerError)
54+
io.WriteString(w, `Internal Server Error`)
55+
}))
56+
defer srv.Close()
57+
58+
ctx := context.Background()
59+
client := NewClient(srv.URL)
60+
61+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
62+
defer cancel()
63+
var responseData map[string]interface{}
64+
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
65+
is.Equal(calls, 1) // calls
66+
is.Equal(err.Error(), "graphql: server returned a non-200 status code: 500")
67+
}
68+
69+
func TestDoJSONBadRequestErr(t *testing.T) {
70+
is := is.New(t)
71+
var calls int
72+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
73+
calls++
74+
is.Equal(r.Method, http.MethodPost)
75+
b, err := ioutil.ReadAll(r.Body)
76+
is.NoErr(err)
77+
is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
78+
w.WriteHeader(http.StatusBadRequest)
79+
io.WriteString(w, `{
80+
"errors": [{
81+
"message": "miscellaneous message as to why the the request was bad"
82+
}]
83+
}`)
84+
}))
85+
defer srv.Close()
86+
87+
ctx := context.Background()
88+
client := NewClient(srv.URL)
89+
90+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
91+
defer cancel()
92+
var responseData map[string]interface{}
93+
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
94+
is.Equal(calls, 1) // calls
95+
is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
96+
}
97+
4498
func TestQueryJSON(t *testing.T) {
4599
is := is.New(t)
46100

graphql_multipart_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,56 @@ func TestDoErr(t *testing.T) {
9090
is.Equal(err.Error(), "graphql: Something went wrong")
9191
}
9292

93+
func TestDoServerErr(t *testing.T) {
94+
is := is.New(t)
95+
var calls int
96+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
97+
calls++
98+
is.Equal(r.Method, http.MethodPost)
99+
query := r.FormValue("query")
100+
is.Equal(query, `query {}`)
101+
w.WriteHeader(http.StatusInternalServerError)
102+
io.WriteString(w, `Internal Server Error`)
103+
}))
104+
defer srv.Close()
105+
106+
ctx := context.Background()
107+
client := NewClient(srv.URL, UseMultipartForm())
108+
109+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
110+
defer cancel()
111+
var responseData map[string]interface{}
112+
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
113+
is.Equal(err.Error(), "graphql: server returned a non-200 status code: 500")
114+
}
115+
116+
func TestDoBadRequestErr(t *testing.T) {
117+
is := is.New(t)
118+
var calls int
119+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
120+
calls++
121+
is.Equal(r.Method, http.MethodPost)
122+
query := r.FormValue("query")
123+
is.Equal(query, `query {}`)
124+
w.WriteHeader(http.StatusBadRequest)
125+
io.WriteString(w, `{
126+
"errors": [{
127+
"message": "miscellaneous message as to why the the request was bad"
128+
}]
129+
}`)
130+
}))
131+
defer srv.Close()
132+
133+
ctx := context.Background()
134+
client := NewClient(srv.URL, UseMultipartForm())
135+
136+
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
137+
defer cancel()
138+
var responseData map[string]interface{}
139+
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
140+
is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
141+
}
142+
93143
func TestDoNoResponse(t *testing.T) {
94144
is := is.New(t)
95145
var calls int

0 commit comments

Comments
 (0)