From ba5d6d53888520ac3f37f155245be9376420dabf Mon Sep 17 00:00:00 2001 From: Gavin Bunney Date: Wed, 9 Oct 2019 11:06:10 -0700 Subject: [PATCH] Handle API errors correctly for Bitbucket server --- bitbucket/client.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/bitbucket/client.go b/bitbucket/client.go index b90daf2..61f6fa1 100644 --- a/bitbucket/client.go +++ b/bitbucket/client.go @@ -12,16 +12,25 @@ import ( // Error represents a error from the bitbucket api. type Error struct { - APIError struct { - Message string `json:"message,omitempty"` - } `json:"error,omitempty"` - Type string `json:"type,omitempty"` + Errors []struct { + Context string `json:"context,omitempty"` + Message string `json:"message,omitempty"` + Exception string `json:"exceptionName,omitempty"` + } `json:"errors,omitempty"` StatusCode int Endpoint string } func (e Error) Error() string { - return fmt.Sprintf("API Error: %d %s %s", e.StatusCode, e.Endpoint, e.APIError.Message) + + var errorMessages = "" + if e.Errors != nil { + for _, err := range e.Errors { + errorMessages += err.Message + "\n" + } + } + + return fmt.Sprintf("API Error: %d %s %s", e.StatusCode, e.Endpoint, errorMessages) } type BitbucketClient struct { @@ -72,11 +81,7 @@ func (c *BitbucketClient) Do(method, endpoint string, payload *bytes.Buffer) (*h log.Printf("[DEBUG] Resp Body: %s", string(body)) - err = json.Unmarshal(body, &apiError) - if err != nil { - apiError.APIError.Message = string(body) - } - + _ = json.Unmarshal(body, &apiError) return resp, error(apiError) }