Skip to content

Commit

Permalink
Merge pull request #4 from drewsonne/master
Browse files Browse the repository at this point in the history
Added more verbose debugging on error
  • Loading branch information
webdevwilson authored Sep 19, 2017
2 parents a12b2da + e3cb175 commit c5841f1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
build:
go build

test: fmtcheck
go test

fmt:
gofmt -w $(GOFMT_FILES)

fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"
60 changes: 40 additions & 20 deletions builtin/providers/artifactory/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
Expand Down Expand Up @@ -174,8 +175,8 @@ func (c clientConfig) GetRepository(key string, v interface{}) error {
return err
}

if resp.StatusCode != 200 {
return fmt.Errorf("Failed to update repository. Status: %s", resp.Status)
if err := c.validateResponse(200, "update repository", resp); err != nil {
return err
}

decoder := json.NewDecoder(resp.Body)
Expand All @@ -196,8 +197,8 @@ func (c clientConfig) CreateRepository(key string, v interface{}) error {
return err
}

if resp.StatusCode != 200 {
return fmt.Errorf("Failed to create repository. Status: %s", resp.Status)
if err := c.validateResponse(200, "create repository", resp); err != nil {
return err
}

return resp.Body.Close()
Expand All @@ -212,8 +213,8 @@ func (c clientConfig) UpdateRepository(key string, v interface{}) error {
return err
}

if resp.StatusCode != 200 {
return fmt.Errorf("Failed to update repository. Status: %s", resp.Status)
if err := c.validateResponse(200, "update repository", resp); err != nil {
return err
}

return resp.Body.Close()
Expand All @@ -228,8 +229,8 @@ func (c clientConfig) DeleteRepository(key string) error {
return err
}

if resp.StatusCode != 200 {
return fmt.Errorf("Failed to delete repository. Status: %s", resp.Status)
if err := c.validateResponse(200, "delete repository", resp); err != nil {
return err
}

return resp.Body.Close()
Expand Down Expand Up @@ -271,8 +272,8 @@ func (c clientConfig) CreateUser(u *User) error {
return err
}

if resp.StatusCode != 201 {
return fmt.Errorf("Failed to create user. Status: %s", resp.Status)
if err := c.validateResponse(201, "create user", resp); err != nil {
return err
}

return resp.Body.Close()
Expand All @@ -287,8 +288,8 @@ func (c clientConfig) UpdateUser(u *User) error {
return err
}

if resp.StatusCode != 200 {
return fmt.Errorf("Failed to update user. Status: %s", resp.Status)
if err := c.validateResponse(200, "update user", resp); err != nil {
return err
}

return resp.Body.Close()
Expand All @@ -303,8 +304,8 @@ func (c clientConfig) DeleteUser(name string) error {
return err
}

if resp.StatusCode != 200 {
return fmt.Errorf("Failed to delete user. Status: %s", resp.Status)
if err := c.validateResponse(200, "delete user", resp); err != nil {
return err
}

return resp.Body.Close()
Expand Down Expand Up @@ -365,8 +366,8 @@ func (c clientConfig) CreateGroup(g *Group) error {
return err
}

if resp.StatusCode != 201 {
return fmt.Errorf("Failed to create group. Status: %s", resp.Status)
if err := c.validateResponse(201, "create group", resp); err != nil {
return err
}

return resp.Body.Close()
Expand All @@ -381,8 +382,8 @@ func (c clientConfig) UpdateGroup(g *Group) error {
return err
}

if resp.StatusCode != 200 {
return fmt.Errorf("Failed to update group. Status: %s", resp.Status)
if err := c.validateResponse(200, "update group", resp); err != nil {
return err
}

return resp.Body.Close()
Expand All @@ -397,8 +398,8 @@ func (c clientConfig) DeleteGroup(name string) error {
return err
}

if resp.StatusCode != 200 {
return fmt.Errorf("Failed to delete group. Status: %s", resp.Status)
if err := c.validateResponse(200, "delete group", resp); err != nil {
return err
}

return resp.Body.Close()
Expand Down Expand Up @@ -428,3 +429,22 @@ func (c clientConfig) execute(method string, endpoint string, payload interface{

return client.Do(req)
}

func (c clientConfig) validateResponse(expectedCode int, action string, resp *http.Response) (err error) {
if resp.StatusCode != expectedCode {
response := ""
if resp, err := ioutil.ReadAll(resp.Body); err == nil {
response = fmt.Sprintf(" Response: %s", string(resp))
}
request := ""
if req, err := ioutil.ReadAll(resp.Request.Body); err == nil {
headers := map[string][]string{}
for name, header := range resp.Request.Header {
headers[name] = header
}
request = fmt.Sprintf(" Request:%s\n%s", headers, string(req))
}
return fmt.Errorf("Failed to %s. Status: %s.%s%s", action, resp.Status, request, response)
}
return nil
}
13 changes: 13 additions & 0 deletions scripts/gofmtcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# Check gofmt
echo "==> Checking that code complies with gofmt requirements..."
gofmt_files=$(gofmt -l `find . -name '*.go' | grep -v vendor`)
if [[ -n ${gofmt_files} ]]; then
echo 'gofmt needs running on the following files:'
echo "${gofmt_files}"
echo "You can use the command: \`make fmt\` to reformat code."
exit 1
fi

exit 0

0 comments on commit c5841f1

Please sign in to comment.