Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 3, 2019
1 parent a3ce15d commit 917d511
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (a *miteApi) get(resource string, result interface{}) error {
}

defer func() { _ = res.Body.Close() }()
if err := a.check(res); err != nil {
return err
}

return json.NewDecoder(res.Body).Decode(result)
}
Expand Down Expand Up @@ -77,6 +80,9 @@ func (a *miteApi) post(resource string, body interface{}, result interface{}) er
}

defer func() { _ = res.Body.Close() }()
if err := a.check(res); err != nil {
return err
}

return json.NewDecoder(res.Body).Decode(result)
}
Expand All @@ -96,10 +102,16 @@ func (a *miteApi) patch(resource string, body interface{}) error {
req.Header.Add("Content-Type", "application/json")

res, err := a.client.Do(req)
if err != nil {
return err
}

defer func() { _ = res.Body.Close() }()
if err := a.check(res); err != nil {
return err
}

return err
return nil
}

func (a *miteApi) delete(resource string) error {
Expand All @@ -111,8 +123,30 @@ func (a *miteApi) delete(resource string) error {
req.Header.Add("User-Agent", userAgent)

res, err := a.client.Do(req)
if err != nil {
return err
}

defer func() { _ = res.Body.Close() }()
if err := a.check(res); err != nil {
return err
}

return nil
}

func (a *miteApi) check(res *http.Response) error {
if res.StatusCode < 400 {
return nil
}

msg := struct {
Error string `json:"error"`
}{}
err := json.NewDecoder(res.Body).Decode(&msg)
if err != nil {
return fmt.Errorf("failed to %s %s", res.Request.Method, res.Request.RequestURI)
}

return err
return fmt.Errorf("failed to %s %s: %s", res.Request.Method, res.Request.RequestURI, msg.Error)
}

0 comments on commit 917d511

Please sign in to comment.