Skip to content

Commit 504759c

Browse files
committed
Merge pull request #5 from contiv/shaleman
Fix http error handling in go client
2 parents 0dcaa4f + c54dce0 commit 504759c

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

generators/templates.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ func httpGet(url string, jdata interface{}) error {
4949
return errors.New("Page not found!")
5050
case r.StatusCode == int(403):
5151
return errors.New("Access denied!")
52+
case r.StatusCode == int(500):
53+
response, err := ioutil.ReadAll(r.Body)
54+
if err != nil {
55+
return err
56+
}
57+
58+
return errors.New(string(response))
59+
5260
case r.StatusCode != int(200):
5361
log.Debugf("GET Status '%s' status code %d \n", r.Status, r.StatusCode)
5462
return errors.New(r.Status)
@@ -84,6 +92,14 @@ func httpDelete(url string) error {
8492
return nil
8593
case r.StatusCode == int(403):
8694
return errors.New("Access denied!")
95+
case r.StatusCode == int(500):
96+
response, err := ioutil.ReadAll(r.Body)
97+
if err != nil {
98+
return err
99+
}
100+
101+
return errors.New(string(response))
102+
87103
case r.StatusCode != int(200):
88104
log.Debugf("DELETE Status '%s' status code %d \n", r.Status, r.StatusCode)
89105
return errors.New(r.Status)
@@ -110,6 +126,14 @@ func httpPost(url string, jdata interface{}) error {
110126
return errors.New("Page not found!")
111127
case r.StatusCode == int(403):
112128
return errors.New("Access denied!")
129+
case r.StatusCode == int(500):
130+
response, err := ioutil.ReadAll(r.Body)
131+
if err != nil {
132+
return err
133+
}
134+
135+
return errors.New(string(response))
136+
113137
case r.StatusCode != int(200):
114138
log.Debugf("POST Status '%s' status code %d \n", r.Status, r.StatusCode)
115139
return errors.New(r.Status)
@@ -148,7 +172,7 @@ func (c *ContivClient) {{ initialCap .Name }}Post(obj *{{ initialCap .Name }}) e
148172
// http post the object
149173
err := httpPost(url, obj)
150174
if err != nil {
151-
log.Errorf("Error creating {{ .Name }} %+v. Err: %v", obj, err)
175+
log.Debugf("Error creating {{ .Name }} %+v. Err: %v", obj, err)
152176
return err
153177
}
154178
@@ -164,7 +188,7 @@ func (c *ContivClient) {{ initialCap .Name }}List() (*[]*{{ initialCap .Name }},
164188
var objList []*{{ initialCap .Name }}
165189
err := httpGet(url, &objList)
166190
if err != nil {
167-
log.Errorf("Error getting {{ .Name }}s. Err: %v", err)
191+
log.Debugf("Error getting {{ .Name }}s. Err: %v", err)
168192
return nil, err
169193
}
170194
@@ -181,7 +205,7 @@ func (c *ContivClient) {{ initialCap .Name }}Get({{range $index, $element := .Ke
181205
var obj {{ initialCap .Name }}
182206
err := httpGet(url, &obj)
183207
if err != nil {
184-
log.Errorf("Error getting {{ .Name }} %+v. Err: %v", keyStr, err)
208+
log.Debugf("Error getting {{ .Name }} %+v. Err: %v", keyStr, err)
185209
return nil, err
186210
}
187211
@@ -197,7 +221,7 @@ func (c *ContivClient) {{ initialCap .Name }}Delete({{range $index, $element :=
197221
// http get the object
198222
err := httpDelete(url)
199223
if err != nil {
200-
log.Errorf("Error deleting {{ .Name }} %s. Err: %v", keyStr, err)
224+
log.Debugf("Error deleting {{ .Name }} %s. Err: %v", keyStr, err)
201225
return err
202226
}
203227

generators/templates/clientHdr.tmpl

+24
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ func httpGet(url string, jdata interface{}) error {
2929
return errors.New("Page not found!")
3030
case r.StatusCode == int(403):
3131
return errors.New("Access denied!")
32+
case r.StatusCode == int(500):
33+
response, err := ioutil.ReadAll(r.Body)
34+
if err != nil {
35+
return err
36+
}
37+
38+
return errors.New(string(response))
39+
3240
case r.StatusCode != int(200):
3341
log.Debugf("GET Status '%s' status code %d \n", r.Status, r.StatusCode)
3442
return errors.New(r.Status)
@@ -64,6 +72,14 @@ func httpDelete(url string) error {
6472
return nil
6573
case r.StatusCode == int(403):
6674
return errors.New("Access denied!")
75+
case r.StatusCode == int(500):
76+
response, err := ioutil.ReadAll(r.Body)
77+
if err != nil {
78+
return err
79+
}
80+
81+
return errors.New(string(response))
82+
6783
case r.StatusCode != int(200):
6884
log.Debugf("DELETE Status '%s' status code %d \n", r.Status, r.StatusCode)
6985
return errors.New(r.Status)
@@ -90,6 +106,14 @@ func httpPost(url string, jdata interface{}) error {
90106
return errors.New("Page not found!")
91107
case r.StatusCode == int(403):
92108
return errors.New("Access denied!")
109+
case r.StatusCode == int(500):
110+
response, err := ioutil.ReadAll(r.Body)
111+
if err != nil {
112+
return err
113+
}
114+
115+
return errors.New(string(response))
116+
93117
case r.StatusCode != int(200):
94118
log.Debugf("POST Status '%s' status code %d \n", r.Status, r.StatusCode)
95119
return errors.New(r.Status)

generators/templates/clientObj.tmpl

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func (c *ContivClient) {{ initialCap .Name }}Post(obj *{{ initialCap .Name }}) e
77
// http post the object
88
err := httpPost(url, obj)
99
if err != nil {
10-
log.Errorf("Error creating {{ .Name }} %+v. Err: %v", obj, err)
10+
log.Debugf("Error creating {{ .Name }} %+v. Err: %v", obj, err)
1111
return err
1212
}
1313

@@ -23,7 +23,7 @@ func (c *ContivClient) {{ initialCap .Name }}List() (*[]*{{ initialCap .Name }},
2323
var objList []*{{ initialCap .Name }}
2424
err := httpGet(url, &objList)
2525
if err != nil {
26-
log.Errorf("Error getting {{ .Name }}s. Err: %v", err)
26+
log.Debugf("Error getting {{ .Name }}s. Err: %v", err)
2727
return nil, err
2828
}
2929

@@ -40,7 +40,7 @@ func (c *ContivClient) {{ initialCap .Name }}Get({{range $index, $element := .Ke
4040
var obj {{ initialCap .Name }}
4141
err := httpGet(url, &obj)
4242
if err != nil {
43-
log.Errorf("Error getting {{ .Name }} %+v. Err: %v", keyStr, err)
43+
log.Debugf("Error getting {{ .Name }} %+v. Err: %v", keyStr, err)
4444
return nil, err
4545
}
4646

@@ -56,7 +56,7 @@ func (c *ContivClient) {{ initialCap .Name }}Delete({{range $index, $element :=
5656
// http get the object
5757
err := httpDelete(url)
5858
if err != nil {
59-
log.Errorf("Error deleting {{ .Name }} %s. Err: %v", keyStr, err)
59+
log.Debugf("Error deleting {{ .Name }} %s. Err: %v", keyStr, err)
6060
return err
6161
}
6262

0 commit comments

Comments
 (0)