change error format

This commit is contained in:
Ryan Cavicchioni 2020-03-27 19:31:12 -05:00
parent 7c7b06e893
commit 8df5b1d779
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
4 changed files with 9 additions and 5 deletions

View File

@ -25,7 +25,7 @@ func LsCmd(args CmdArgs) int {
selector := args.Flags.String("selector") selector := args.Flags.String("selector")
lights, err := c.ListLights(selector) lights, err := c.ListLights(selector)
if err != nil { if err != nil {
fmt.Println(err) fmt.Printf("fatal: %s\n", err)
return 1 return 1
} }
PrintLights(lights) PrintLights(lights)

View File

@ -47,7 +47,7 @@ func main() {
cmd, ok := lumecmd.GetCommand(command) cmd, ok := lumecmd.GetCommand(command)
if !ok { if !ok {
fmt.Println("ERROR") fmt.Printf("lume: '%s' is not lume command. See 'lume' --help.'\n", command)
os.Exit(1) os.Exit(1)
} }
fs := cmd.Flags fs := cmd.Flags

View File

@ -24,7 +24,7 @@ func ToggleCmd(args CmdArgs) int {
selector := args.Flags.String("selector") selector := args.Flags.String("selector")
r, err := c.Toggle(selector, duration) r, err := c.Toggle(selector, duration)
if err != nil { if err != nil {
fmt.Println(err) fmt.Printf("fatal: %s\n", err)
return 1 return 1
} }
PrintResults(r.Results) PrintResults(r.Results)

View File

@ -3,7 +3,7 @@ package lifx
import ( import (
//"crypto/tls" //"crypto/tls"
"encoding/json" "encoding/json"
"fmt" "errors"
"net/http" "net/http"
"time" "time"
) )
@ -97,7 +97,7 @@ func NewApiError(resp *http.Response) error {
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil { if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
return err return err
} }
return fmt.Errorf("fatal: %s", s.Error) return errors.New(s.Error)
} }
func IsApiError(resp *http.Response) bool { func IsApiError(resp *http.Response) bool {
@ -120,6 +120,10 @@ func (c *Client) SetState(selector string, state State) (*Response, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
if IsApiError(resp) {
return nil, NewApiError(resp)
}
if state.Fast && resp.StatusCode == http.StatusAccepted { if state.Fast && resp.StatusCode == http.StatusAccepted {
return nil, nil return nil, nil
} }