error helpers

This commit is contained in:
Ryan Cavicchioni 2020-03-26 21:05:11 -05:00
parent bba7c22460
commit 917053f39c
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D

View File

@ -89,7 +89,7 @@ type (
} }
) )
func NewAPIError(resp *http.Response) error { func NewApiError(resp *http.Response) error {
var ( var (
s *Response s *Response
err error err error
@ -100,6 +100,10 @@ func NewAPIError(resp *http.Response) error {
return fmt.Errorf("fatal: %s", s.Error) return fmt.Errorf("fatal: %s", s.Error)
} }
func IsApiError(resp *http.Response) bool {
return resp.StatusCode > 299
}
func (s Status) Success() bool { func (s Status) Success() bool {
return s == OK return s == OK
} }
@ -182,8 +186,8 @@ func (c *Client) Toggle(selector string, duration float64) (*Response, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode > 299 { if IsApiError(resp) {
return nil, NewAPIError(resp) return nil, NewApiError(resp)
} }
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
@ -206,7 +210,7 @@ func (c *Client) ListLights(selector string) ([]Light, error) {
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode > 299 { if resp.StatusCode > 299 {
return nil, NewAPIError(resp) return nil, NewApiError(resp)
} }
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {