From bd7704a5f0096af9d4da05a7a0e88aae4fc898cc Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Tue, 4 Dec 2018 22:57:38 +0100 Subject: [PATCH] Client: handle 409 HTTP code necessary as POST /api/v1/releases returns no HTTP body for a 409 request, eg when the target_comitish already has a release. The underlying problem is bigger: The Client cannot handle error responses which do not contain a valid JSON body. --- gitea/gitea.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gitea/gitea.go b/gitea/gitea.go index 374a3fa..a5509b2 100644 --- a/gitea/gitea.go +++ b/gitea/gitea.go @@ -70,6 +70,8 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re return nil, errors.New("403 Forbidden") case 404: return nil, errors.New("404 Not Found") + case 409: + return nil, errors.New("409 Conflict") case 422: return nil, fmt.Errorf("422 Unprocessable Entity: %s", string(data)) } @@ -77,7 +79,9 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re if resp.StatusCode/100 != 2 { errMap := make(map[string]interface{}) if err = json.Unmarshal(data, &errMap); err != nil { - return nil, err + // when the JSON can't be parsed, data was probably empty or a plain string, + // so we try to return a helpful error anyway + return nil, fmt.Errorf("Unknown API Error: %d %s", resp.StatusCode, string(data)) } return nil, errors.New(errMap["message"].(string)) }