haproxy-lb/pkg/api/client/helper.go
2021-04-10 01:37:30 +02:00

37 lines
640 B
Go

package client
import (
"encoding/json"
"errors"
"net/http"
)
type Error struct {
Message string `json:"message"`
Status int `json:"status"`
}
func decodeResp(resp *http.Response, v interface{}) error {
if resp.Body == nil {
return nil
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
if v == nil {
return nil
}
return json.NewDecoder(resp.Body).Decode(&v)
}
if resp.StatusCode == 404 {
return ErrNotFound
}
var e Error
err := json.NewDecoder(resp.Body).Decode(&e)
if err != nil {
return err
}
if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
return errors.New(e.Message)
}
return nil
}