Skip to content

Commit c33d682

Browse files
committed
Better error handling
1 parent f5f5d12 commit c33d682

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

cmd/kefw2/cmd/next_track.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var nextTrackCmd = &cobra.Command{
1616
Run: func(cmd *cobra.Command, args []string) {
1717
canControlPlayback, err := currentSpeaker.CanControlPlayback()
1818
if err != nil {
19-
fmt.Printf("Can't query source: %s\n", err.Error())
19+
fmt.Printf("Can't skip track: %s\n", err.Error())
2020
os.Exit(1)
2121
}
2222
if !canControlPlayback {

cmd/kefw2/cmd/pause.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var pauseCmd = &cobra.Command{
1616
Run: func(cmd *cobra.Command, args []string) {
1717
canControlPlayback, err := currentSpeaker.CanControlPlayback()
1818
if err != nil {
19-
fmt.Printf("Can't query source: %s\n", err.Error())
19+
fmt.Printf("Can't pause speaker: %s\n", err.Error())
2020
os.Exit(1)
2121
}
2222
if !canControlPlayback {

cmd/kefw2/cmd/prevous_track.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var previousTrackCmd = &cobra.Command{
1717
Run: func(cmd *cobra.Command, args []string) {
1818
canControlPlayback, err := currentSpeaker.CanControlPlayback()
1919
if err != nil {
20-
fmt.Printf("Can't query source: %s\n", err.Error())
20+
fmt.Printf("Can't skip back: %s\n", err.Error())
2121
os.Exit(1)
2222
}
2323
if !canControlPlayback {

cmd/kefw2/cmd/status.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var statusCmd = &cobra.Command{
2626
}
2727
canControlPlayback, err := currentSpeaker.CanControlPlayback()
2828
if err != nil {
29-
fmt.Printf("Can't query source: %s\n", err.Error())
29+
fmt.Printf("Can't show status: %s\n", err.Error())
3030
os.Exit(1)
3131
}
3232
if canControlPlayback {
@@ -62,6 +62,7 @@ var statusCmd = &cobra.Command{
6262
// Not so minimalistic output
6363
if minimal, _ := cmd.Flags().GetBool("minimal"); !minimal {
6464
icat.PrintImageURL(pd.TrackRoles.Icon)
65+
fmt.Println() // newline so shell prompt does not appear to the right of the image
6566
}
6667
} else {
6768
fmt.Println("Audio Transport: stopped")

kefw2/http.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8+
"net"
89
"net/http"
10+
"strings"
911
"time"
1012

1113
log "github.com/sirupsen/logrus"
@@ -17,8 +19,32 @@ type KEFPostRequest struct {
1719
Value *json.RawMessage `json:"value"`
1820
}
1921

22+
func (s KEFSpeaker) handleConnectionError(err error) error {
23+
if err == nil {
24+
return nil
25+
}
26+
27+
errStr := err.Error()
28+
if nerr, ok := err.(net.Error); ok {
29+
if nerr.Timeout() {
30+
return fmt.Errorf("Connection timed out when trying to reach speaker at %s. Please check if the speaker is available and responsive.", s.IPAddress)
31+
}
32+
fmt.Println("nerr:", nerr)
33+
}
34+
// fmt.Println(
35+
if strings.Contains(errStr, "connection refused") {
36+
return fmt.Errorf("Unable to connect to speaker at %s. Please ensure the speaker is powered on and connected to the network.", s.IPAddress)
37+
}
38+
if strings.Contains(errStr, "timeout") {
39+
return fmt.Errorf("Connection timed out when trying to reach speaker at %s. Please check if the speaker is available and responsive.", s.IPAddress)
40+
}
41+
if strings.Contains(errStr, "no such host") {
42+
return fmt.Errorf("Could not find speaker at %s. Please check if the IP address is correct", s.IPAddress)
43+
}
44+
return fmt.Errorf("Connection error: %v", err)
45+
}
46+
2047
func (s KEFSpeaker) getData(path string) ([]byte, error) {
21-
// log.SetLevel(log.DebugLevel)
2248
client := &http.Client{}
2349
client.Timeout = 1.0 * time.Second
2450

@@ -36,7 +62,7 @@ func (s KEFSpeaker) getData(path string) ([]byte, error) {
3662

3763
resp, err := client.Do(req)
3864
if err != nil {
39-
return nil, err
65+
return nil, s.handleConnectionError(err)
4066
}
4167
defer resp.Body.Close()
4268

@@ -54,7 +80,6 @@ func (s KEFSpeaker) getData(path string) ([]byte, error) {
5480
}
5581

5682
func (s KEFSpeaker) getAllData(path string) ([]byte, error) {
57-
// log.SetLevel(log.DebugLevel)
5883
client := &http.Client{}
5984
client.Timeout = 1.0 * time.Second
6085

@@ -69,10 +94,10 @@ func (s KEFSpeaker) getAllData(path string) ([]byte, error) {
6994
q.Add("path", path)
7095
q.Add("roles", "@all")
7196
req.URL.RawQuery = q.Encode()
72-
fmt.Println("Request URL:", req.URL.String())
97+
7398
resp, err := client.Do(req)
7499
if err != nil {
75-
return nil, err
100+
return nil, s.handleConnectionError(err)
76101
}
77102
defer resp.Body.Close()
78103

@@ -109,7 +134,7 @@ func (s KEFSpeaker) getRows(path string, params map[string]string) ([]byte, erro
109134

110135
resp, err := client.Do(req)
111136
if err != nil {
112-
return nil, err
137+
return nil, s.handleConnectionError(err)
113138
}
114139
defer resp.Body.Close()
115140

@@ -151,7 +176,7 @@ func (s KEFSpeaker) setActivate(path, item, value string) error {
151176

152177
resp, err := client.Do(req)
153178
if err != nil {
154-
return err
179+
return s.handleConnectionError(err)
155180
}
156181
defer resp.Body.Close()
157182

@@ -215,7 +240,7 @@ func (s KEFSpeaker) setTypedValue(path string, value any) error {
215240

216241
resp, err := client.Do(req)
217242
if err != nil {
218-
return err
243+
return s.handleConnectionError(err)
219244
}
220245
defer resp.Body.Close()
221246

kefw2/kefw2.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,19 @@ func (s KEFSpeaker) SetSource(source Source) error {
163163

164164
func (s *KEFSpeaker) Source() (Source, error) {
165165
data, err := s.getData("settings:/kef/play/physicalSource")
166+
if err != nil {
167+
return SourceStandby, fmt.Errorf("Failed getting speaker source: %w", err)
168+
}
166169
src, err2 := JSONUnmarshalValue(data, err)
167170
return src.(Source), err2
168171
}
169172

170173
func (s *KEFSpeaker) CanControlPlayback() (bool, error) {
171174
source, err := s.Source()
172175
if err != nil {
173-
return false, fmt.Errorf("failed getting speaker source: %w", err)
176+
return false, err
174177
}
175-
return (source != SourceWiFi || source != SourceBluetooth), nil
178+
return ((source == SourceWiFi) || (source == SourceBluetooth)), nil
176179
}
177180

178181
func (s *KEFSpeaker) IsPoweredOn() (bool, error) {

0 commit comments

Comments
 (0)