Skip to content

Commit

Permalink
Merge pull request #175 from CircleCI-Public/errors
Browse files Browse the repository at this point in the history
Fail earlier when HTTP calls fail
  • Loading branch information
Zachary Scott authored Oct 30, 2018
2 parents 6437fd1 + 525ab9a commit f146a5d
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ func (cl *Client) Run(ctx context.Context, log *logger.Logger, request *Request,
}
}()

log.Debug(">> result status: %s", res.Status)

if res.StatusCode != 200 {
return fmt.Errorf("failure calling GraphQL API: %s", res.Status)
}

wrappedResponse := &Response{
Data: resp,
}
Expand Down
104 changes: 104 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,107 @@ func TestHeader(t *testing.T) {
t.Errorf("expected %+v", resp)
}
}

func TestStatusCode200(t *testing.T) {
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++

w.WriteHeader(http.StatusOK)

_, err := io.WriteString(w, `{"data":{"value":"some data"}}`)
if err != nil {
t.Errorf(err.Error())
}
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

client := NewClient(srv.URL, "/", "token")

req := NewUnauthorizedRequest("query {}")

var resp interface{}

err := client.Run(ctx, log, req, &resp)
if err != nil {
t.Errorf(err.Error())
}

if calls != 1 {
t.Errorf("expected %s", string(calls))
}
}

func TestStatusCode500(t *testing.T) {
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++

w.WriteHeader(500)

_, err := io.WriteString(w, "some: data")
if err != nil {
t.Errorf(err.Error())
}
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

client := NewClient(srv.URL, "/", "token")

req := NewUnauthorizedRequest("query {}")

var resp interface{}

err := client.Run(ctx, log, req, &resp)
if err == nil {
t.Error("expected error")
}

if err.Error() != "failure calling GraphQL API: 500 Internal Server Error" {
t.Errorf("expected %s", err.Error())
}

if calls != 1 {
t.Errorf("expected %s", string(calls))
}
}

func TestStatusCode413(t *testing.T) {
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++

w.WriteHeader(413)

_, err := io.WriteString(w, "some: data")
if err != nil {
t.Errorf(err.Error())
}
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

client := NewClient(srv.URL, "/", "token")

req := NewUnauthorizedRequest("query {}")

var resp interface{}

err := client.Run(ctx, log, req, &resp)
if err == nil {
t.Error("expected error")
}

if err.Error() != "failure calling GraphQL API: 413 Request Entity Too Large" {
t.Errorf("expected %s", err.Error())
}

if calls != 1 {
t.Errorf("expected %s", string(calls))
}
}

0 comments on commit f146a5d

Please sign in to comment.