Move error code out to error.go

This commit is contained in:
Jingwen Owen Ou 2013-06-08 12:12:48 -07:00
Родитель 502f6fbe35
Коммит c805d78f90
4 изменённых файлов: 73 добавлений и 51 удалений

59
octokit/error.go Normal file
Просмотреть файл

@ -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
}

13
octokit/error_test.go Normal file
Просмотреть файл

@ -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())
}

Просмотреть файл

@ -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))

Просмотреть файл

@ -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 := "<h1>\n<a name=\"title\" class=\"anchor\" href=\"#title\"><span class=\"octicon octicon-link\"></span></a>title</h1>"