From c805d78f9026dd0f4e1c77cc53b81d26e33bb751 Mon Sep 17 00:00:00 2001 From: Jingwen Owen Ou Date: Sat, 8 Jun 2013 12:12:48 -0700 Subject: [PATCH] Move error code out to error.go --- octokit/error.go | 59 +++++++++++++++++++++++++++++++++++++++++ octokit/error_test.go | 13 +++++++++ octokit/octokit.go | 47 -------------------------------- octokit/octokit_test.go | 5 +--- 4 files changed, 73 insertions(+), 51 deletions(-) create mode 100644 octokit/error.go create mode 100644 octokit/error_test.go diff --git a/octokit/error.go b/octokit/error.go new file mode 100644 index 00000000..963d1ed9 --- /dev/null +++ b/octokit/error.go @@ -0,0 +1,59 @@ +package octokit + +import ( + "encoding/json" + "errors" + "fmt" + "strings" +) + +type gitHubError struct { + Resource string `json:"resource"` + Field string `json:"field"` + Value interface{} `json:"value"` + Code string `json:"code"` + Message string `json:"message"` +} + +type gitHubErrors struct { + Message string `json:"message"` + Errors []gitHubError `json:"errors"` +} + +func handleErrors(body []byte) error { + var githubErrors gitHubErrors + err := json.Unmarshal(body, &githubErrors) + if err != nil { + return err + } + + msg := buildErrorMessage(githubErrors) + + return errors.New(msg) +} + +func buildErrorMessage(githubErrors gitHubErrors) string { + errorMessages := make([]string, len(githubErrors.Errors)) + for _, e := range githubErrors.Errors { + switch e.Code { + case "custom": + errorMessages = append(errorMessages, e.Message) + case "missing_field": + msg := fmt.Sprintf("Missing field: %s", e.Field) + errorMessages = append(errorMessages, msg) + case "invalid": + msg := fmt.Sprintf("Invalid value for %s: %v", e.Field, e.Value) + errorMessages = append(errorMessages, msg) + case "unauthorized": + msg := fmt.Sprintf("Not allow to change field %s", e.Field) + errorMessages = append(errorMessages, msg) + } + } + + msg := strings.Join(errorMessages, "\n") + if msg == "" { + msg = githubErrors.Message + } + + return msg +} diff --git a/octokit/error_test.go b/octokit/error_test.go new file mode 100644 index 00000000..13d3054a --- /dev/null +++ b/octokit/error_test.go @@ -0,0 +1,13 @@ +package octokit + +import ( + "github.com/bmizerany/assert" + "testing" +) + +func TestHandleErrors(t *testing.T) { + body := "{\"message\":\"Invalid request media type (expecting 'text/plain')\"}" + err := handleErrors([]byte(body)) + + assert.Equal(t, "Invalid request media type (expecting 'text/plain')", err.Error()) +} diff --git a/octokit/octokit.go b/octokit/octokit.go index fc63a0ec..744965d0 100644 --- a/octokit/octokit.go +++ b/octokit/octokit.go @@ -3,13 +3,10 @@ package octokit import ( "bytes" "encoding/base64" - "encoding/json" - "errors" "fmt" "io" "io/ioutil" "net/http" - "strings" ) const ( @@ -19,19 +16,6 @@ const ( OAuthAppUrl string = "http://owenou.com/gh" ) -type GitHubError struct { - Resource string `json:"resource"` - Field string `json:"field"` - Value interface{} `json:"value"` - Code string `json:"code"` - Message string `json:"message"` -} - -type GitHubErrors struct { - Message string `json:"message"` - Errors []GitHubError `json:"errors"` -} - type Client struct { httpClient *http.Client Login string @@ -89,37 +73,6 @@ func (c *Client) setDefaultHeaders(request *http.Request) { } } -func handleErrors(body []byte) error { - var githubErrors GitHubErrors - err := json.Unmarshal(body, &githubErrors) - if err != nil { - return err - } - - errorMessages := make([]string, len(githubErrors.Errors)) - for _, e := range githubErrors.Errors { - switch e.Code { - case "custom": - errorMessages = append(errorMessages, e.Message) - case "missing_field": - msg := fmt.Sprintf("Missing field: %s", e.Field) - errorMessages = append(errorMessages, msg) - case "invalid": - msg := fmt.Sprintf("Invalid value for %s: %v", e.Field, e.Value) - errorMessages = append(errorMessages, msg) - case "unauthorized": - errorMessages = append(errorMessages, "Not allow to change field "+e.Field) - } - } - - text := strings.Join(errorMessages, "\n") - if text == "" { - text = githubErrors.Message - } - - return errors.New(text) -} - func hashAuth(u, p string) string { var a = fmt.Sprintf("%s:%s", u, p) return base64.StdEncoding.EncodeToString([]byte(a)) diff --git a/octokit/octokit_test.go b/octokit/octokit_test.go index 7aa996e5..351a2d8d 100644 --- a/octokit/octokit_test.go +++ b/octokit/octokit_test.go @@ -18,12 +18,9 @@ func TestPost(t *testing.T) { content := "# title" c := NewClient() - body, err := c.post("markdown/raw", nil, bytes.NewBufferString(content)) - assert.Equal(t, "Invalid request media type (expecting 'text/plain')", err.Error()) - headers := make(map[string]string) headers["Content-Type"] = "text/plain" - body, err = c.post("markdown/raw", headers, bytes.NewBufferString(content)) + body, err := c.post("markdown/raw", headers, bytes.NewBufferString(content)) assert.Equal(t, nil, err) expectBody := "

\ntitle

"