Skip to content

Commit 30e7b2f

Browse files
author
Keerthan Mala
committed
feat(controller):Show the error message from controller instead of resposnse code
1 parent adfa401 commit 30e7b2f

2 files changed

Lines changed: 51 additions & 14 deletions

File tree

pkg/gitreceive/controller.go

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io/ioutil"
78
"net/http"
89
"regexp"
910
"strings"
@@ -17,18 +18,16 @@ var (
1718
potentialExploit = regexp.MustCompile(`\(\)\s+\{[^\}]+\};\s+(.*)`)
1819
)
1920

20-
type unexpectedControllerStatusCode struct {
21-
endpoint string
22-
expected int
23-
actual int
21+
type unexpectedControllerError struct {
22+
errorMsg string
2423
}
2524

26-
func newUnexpectedControllerStatusCode(endpoint string, expectedCode, actualCode int) unexpectedControllerStatusCode {
27-
return unexpectedControllerStatusCode{endpoint: endpoint, expected: expectedCode, actual: actualCode}
25+
func newUnexpectedControllerError(errorMsg string) unexpectedControllerError {
26+
return unexpectedControllerError{errorMsg: errorMsg}
2827
}
2928

30-
func (u unexpectedControllerStatusCode) Error() string {
31-
return fmt.Sprintf("Deis controller endpoint %s: expected status code %d, got %d", u.endpoint, u.expected, u.actual)
29+
func (u unexpectedControllerError) Error() string {
30+
return fmt.Sprintf("Unexpected error occurred: %s", u.errorMsg)
3231
}
3332

3433
func controllerURLStr(conf *Config, additionalPath ...string) string {
@@ -67,9 +66,20 @@ func getAppConfig(conf *Config, builderKey, userName, appName string) (*pkg.Conf
6766
}
6867
defer res.Body.Close()
6968

70-
if res.StatusCode != 200 {
71-
return nil, newUnexpectedControllerStatusCode(url, 200, res.StatusCode)
69+
if res.StatusCode < 200 || res.StatusCode > 299 {
70+
errMsg := new(pkg.ControllerErrorResponse)
71+
if err := json.NewDecoder(res.Body).Decode(errMsg); err != nil {
72+
//If an error occurs decoding the json print the whole response body
73+
respBody, err := ioutil.ReadAll(res.Body)
74+
if err != nil {
75+
return nil, err
76+
}
77+
return nil, newUnexpectedControllerError(string(respBody))
78+
}
79+
80+
return nil, newUnexpectedControllerError(errMsg.ErrorMsg)
7281
}
82+
7383
ret := &pkg.Config{}
7484
if err := json.NewDecoder(res.Body).Decode(ret); err != nil {
7585
return nil, err
@@ -104,8 +114,18 @@ func publishRelease(conf *Config, builderKey string, buildHook *pkg.BuildHook) (
104114

105115
defer res.Body.Close()
106116

107-
if res.StatusCode != 200 {
108-
return nil, newUnexpectedControllerStatusCode(url, 200, res.StatusCode)
117+
if res.StatusCode < 200 || res.StatusCode > 299 {
118+
errMsg := new(pkg.ControllerErrorResponse)
119+
if err := json.NewDecoder(res.Body).Decode(errMsg); err != nil {
120+
//If an error occurs decoding the json print the whole response body
121+
respBody, err := ioutil.ReadAll(res.Body)
122+
if err != nil {
123+
return nil, err
124+
}
125+
return nil, newUnexpectedControllerError(string(respBody))
126+
}
127+
128+
return nil, newUnexpectedControllerError(errMsg.ErrorMsg)
109129
}
110130

111131
ret := new(pkg.BuildHookResponse)
@@ -144,9 +164,21 @@ func receive(conf *Config, builderKey, gitSha string) error {
144164
return err
145165
}
146166
defer resp.Body.Close()
147-
if resp.StatusCode != 201 {
148-
return newUnexpectedControllerStatusCode(urlStr, 201, resp.StatusCode)
167+
168+
if resp.StatusCode < 200 || resp.StatusCode > 299 {
169+
errMsg := new(pkg.ControllerErrorResponse)
170+
if err := json.NewDecoder(resp.Body).Decode(errMsg); err != nil {
171+
//If an error occurs decoding the json print the whole response body
172+
respBody, err := ioutil.ReadAll(resp.Body)
173+
if err != nil {
174+
return err
175+
}
176+
return newUnexpectedControllerError(string(respBody))
177+
}
178+
179+
return newUnexpectedControllerError(errMsg.ErrorMsg)
149180
}
181+
150182
return nil
151183
}
152184

pkg/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ type BuildHookResponse struct {
3131
Release map[string]int `json:"release"`
3232
}
3333

34+
// ControllerErrorResponse represents a controller's error response object.
35+
type ControllerErrorResponse struct {
36+
ErrorMsg string `json:"detail"`
37+
}
38+
3439
// Config represents a Deis application's configuration.
3540
type Config struct {
3641
Owner string `json:"owner"`

0 commit comments

Comments
 (0)