Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hilli committed Feb 15, 2025
1 parent f5f5d12 commit c33d682
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/kefw2/cmd/next_track.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var nextTrackCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
canControlPlayback, err := currentSpeaker.CanControlPlayback()
if err != nil {
fmt.Printf("Can't query source: %s\n", err.Error())
fmt.Printf("Can't skip track: %s\n", err.Error())
os.Exit(1)
}
if !canControlPlayback {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kefw2/cmd/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var pauseCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
canControlPlayback, err := currentSpeaker.CanControlPlayback()
if err != nil {
fmt.Printf("Can't query source: %s\n", err.Error())
fmt.Printf("Can't pause speaker: %s\n", err.Error())
os.Exit(1)
}
if !canControlPlayback {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kefw2/cmd/prevous_track.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var previousTrackCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
canControlPlayback, err := currentSpeaker.CanControlPlayback()
if err != nil {
fmt.Printf("Can't query source: %s\n", err.Error())
fmt.Printf("Can't skip back: %s\n", err.Error())
os.Exit(1)
}
if !canControlPlayback {
Expand Down
3 changes: 2 additions & 1 deletion cmd/kefw2/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var statusCmd = &cobra.Command{
}
canControlPlayback, err := currentSpeaker.CanControlPlayback()
if err != nil {
fmt.Printf("Can't query source: %s\n", err.Error())
fmt.Printf("Can't show status: %s\n", err.Error())
os.Exit(1)
}
if canControlPlayback {
Expand Down Expand Up @@ -62,6 +62,7 @@ var statusCmd = &cobra.Command{
// Not so minimalistic output
if minimal, _ := cmd.Flags().GetBool("minimal"); !minimal {
icat.PrintImageURL(pd.TrackRoles.Icon)
fmt.Println() // newline so shell prompt does not appear to the right of the image
}
} else {
fmt.Println("Audio Transport: stopped")
Expand Down
41 changes: 33 additions & 8 deletions kefw2/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"

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

func (s KEFSpeaker) handleConnectionError(err error) error {
if err == nil {
return nil
}

errStr := err.Error()
if nerr, ok := err.(net.Error); ok {
if nerr.Timeout() {
return fmt.Errorf("Connection timed out when trying to reach speaker at %s. Please check if the speaker is available and responsive.", s.IPAddress)
}
fmt.Println("nerr:", nerr)
}
// fmt.Println(
if strings.Contains(errStr, "connection refused") {
return fmt.Errorf("Unable to connect to speaker at %s. Please ensure the speaker is powered on and connected to the network.", s.IPAddress)
}
if strings.Contains(errStr, "timeout") {
return fmt.Errorf("Connection timed out when trying to reach speaker at %s. Please check if the speaker is available and responsive.", s.IPAddress)
}
if strings.Contains(errStr, "no such host") {
return fmt.Errorf("Could not find speaker at %s. Please check if the IP address is correct", s.IPAddress)
}
return fmt.Errorf("Connection error: %v", err)
}

func (s KEFSpeaker) getData(path string) ([]byte, error) {
// log.SetLevel(log.DebugLevel)
client := &http.Client{}
client.Timeout = 1.0 * time.Second

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

resp, err := client.Do(req)
if err != nil {
return nil, err
return nil, s.handleConnectionError(err)
}
defer resp.Body.Close()

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

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

Expand All @@ -69,10 +94,10 @@ func (s KEFSpeaker) getAllData(path string) ([]byte, error) {
q.Add("path", path)
q.Add("roles", "@all")
req.URL.RawQuery = q.Encode()
fmt.Println("Request URL:", req.URL.String())

resp, err := client.Do(req)
if err != nil {
return nil, err
return nil, s.handleConnectionError(err)
}
defer resp.Body.Close()

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

resp, err := client.Do(req)
if err != nil {
return nil, err
return nil, s.handleConnectionError(err)
}
defer resp.Body.Close()

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

resp, err := client.Do(req)
if err != nil {
return err
return s.handleConnectionError(err)
}
defer resp.Body.Close()

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

resp, err := client.Do(req)
if err != nil {
return err
return s.handleConnectionError(err)
}
defer resp.Body.Close()

Expand Down
7 changes: 5 additions & 2 deletions kefw2/kefw2.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,19 @@ func (s KEFSpeaker) SetSource(source Source) error {

func (s *KEFSpeaker) Source() (Source, error) {
data, err := s.getData("settings:/kef/play/physicalSource")
if err != nil {
return SourceStandby, fmt.Errorf("Failed getting speaker source: %w", err)
}
src, err2 := JSONUnmarshalValue(data, err)
return src.(Source), err2
}

func (s *KEFSpeaker) CanControlPlayback() (bool, error) {
source, err := s.Source()
if err != nil {
return false, fmt.Errorf("failed getting speaker source: %w", err)
return false, err
}
return (source != SourceWiFi || source != SourceBluetooth), nil
return ((source == SourceWiFi) || (source == SourceBluetooth)), nil
}

func (s *KEFSpeaker) IsPoweredOn() (bool, error) {
Expand Down

0 comments on commit c33d682

Please sign in to comment.