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
3433func 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
0 commit comments