From 3ea582bd93a708f7aabab046def3520c5b7a722b Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Thu, 22 Dec 2016 15:31:48 -0700 Subject: [PATCH] lfsapi: expose DecodeJSON helper --- lfsapi/errors.go | 6 +++++- lfsapi/lfsapi.go | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lfsapi/errors.go b/lfsapi/errors.go index 58b13eeb..d4deff10 100644 --- a/lfsapi/errors.go +++ b/lfsapi/errors.go @@ -30,7 +30,11 @@ func (c *Client) handleResponse(res *http.Response) error { } cliErr := &ClientError{} - err := decodeResponse(res, cliErr) + err := DecodeJSON(res, cliErr) + if IsDecodeTypeError(err) { + err = nil + } + if err == nil { if len(cliErr.Message) == 0 { err = defaultError(res) diff --git a/lfsapi/lfsapi.go b/lfsapi/lfsapi.go index 4ea7636e..73ae2e96 100644 --- a/lfsapi/lfsapi.go +++ b/lfsapi/lfsapi.go @@ -2,6 +2,7 @@ package lfsapi import ( "encoding/json" + "fmt" "io" "net/http" "regexp" @@ -89,10 +90,25 @@ func NewClient(osEnv env, gitEnv env) (*Client, error) { return c, nil } -func decodeResponse(res *http.Response, obj interface{}) error { +func IsDecodeTypeError(err error) bool { + _, ok := err.(*decodeTypeError) + return ok +} + +type decodeTypeError struct { + Type string +} + +func (e *decodeTypeError) TypeError() {} + +func (e *decodeTypeError) Error() string { + return fmt.Sprintf("Expected json type, got: %q", e.Type) +} + +func DecodeJSON(res *http.Response, obj interface{}) error { ctype := res.Header.Get("Content-Type") if !(lfsMediaTypeRE.MatchString(ctype) || jsonMediaTypeRE.MatchString(ctype)) { - return nil + return &decodeTypeError{Type: ctype} } err := json.NewDecoder(res.Body).Decode(obj)