Skip to content

Commit 5d28220

Browse files
authored
Fix panic in GetEQProfileV2 and improve error handling (#18)
* Fix refactor2 * Fix panic in GetEQProfileV2 and handle errors properly - Add error checking before type assertion to prevent nil interface panic - Change DeskModeSetting from int to float32 to match API response - Handle and display errors in eq_profile command instead of ignoring them
1 parent 7a708ec commit 5d28220

4 files changed

Lines changed: 21 additions & 7 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ bin
44
share
55
fyne-cross
66
dist/
7-
completions/
7+
completions/
8+
research
9+

cmd/kefw2/cmd/eq_profile.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ var eqProfileCmd = &cobra.Command{
3535
Args: cobra.MaximumNArgs(1),
3636
Run: func(cmd *cobra.Command, _ []string) {
3737
ctx := cmd.Context()
38-
eqProfile, _ := currentSpeaker.GetEQProfileV2(ctx)
38+
eqProfile, err := currentSpeaker.GetEQProfileV2(ctx)
39+
if err != nil {
40+
fmt.Fprintf(cmd.ErrOrStderr(), "Error getting EQ profile: %v\n", err)
41+
return
42+
}
3943
fmt.Println(eqProfile)
4044
},
4145
}

kefw2/eqprofilev2.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type EQProfileV2 struct {
1313
Balance int `json:"balance"` // Left/right balance (-10 to +10)
1414
BassExtension string `json:"bassExtension"` // Bass extension mode: "less", "standard", or "more"
1515
DeskMode bool `json:"deskMode"` // Whether desk mode is enabled
16-
DeskModeSetting int `json:"deskModeSetting"` // Desk mode compensation level
16+
DeskModeSetting float32 `json:"deskModeSetting"` // Desk mode compensation level
1717
HighPassMode bool `json:"highPassMode"` // Whether high-pass filter is enabled (for use with subwoofer)
1818
HighPassModeFreq int `json:"highPassModeFreq"` // High-pass filter frequency in Hz
1919
IsExpertMode bool `json:"isExpertMode"` // Whether expert mode is enabled
@@ -38,7 +38,14 @@ type EQProfileV2 struct {
3838
// Each source can have its own EQ profile settings.
3939
func (s *KEFSpeaker) GetEQProfileV2(ctx context.Context) (EQProfileV2, error) {
4040
eqProfile, err := JSONUnmarshalValue(s.getData(ctx, "kef:eqProfile/v2"))
41-
return eqProfile.(EQProfileV2), err
41+
if err != nil {
42+
return EQProfileV2{}, err
43+
}
44+
profile, ok := eqProfile.(EQProfileV2)
45+
if !ok {
46+
return EQProfileV2{}, ErrUnknownType
47+
}
48+
return profile, nil
4249
}
4350

4451
// String returns the EQ profile as a formatted JSON string.

kefw2/http.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ const (
2929

3030
// Common errors.
3131
var (
32-
ErrConnectionRefused = errors.New("connection refused")
33-
ErrConnectionTimeout = errors.New("connection timed out")
34-
ErrHostNotFound = errors.New("host not found")
32+
ErrConnectionRefused = errors.New("connection refused")
33+
ErrConnectionTimeout = errors.New("connection timed out")
34+
ErrHostNotFound = errors.New("host not found")
35+
ErrInvalidJSONResponse = errors.New("invalid JSON response")
3536
)
3637

3738
// KEFPostRequest represents the JSON structure for POST requests to the KEF API.

0 commit comments

Comments
 (0)