error helpers
This commit is contained in:
parent
bba7c22460
commit
917053f39c
12
lights.go
12
lights.go
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user