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+
2047func (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
5682func (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
0 commit comments