Skip to content

Commit a2c4341

Browse files
committed
handle empty responses
1 parent d562c1a commit a2c4341

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

pkg/client/client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ func (c *Conn) PowerStatus(ctx context.Context) (PowerStatus, error) {
205205
// OnPowerStatus wraps the given callback function into the ResponseHandler interface and translates the generic response into a power status.
206206
func OnPowerStatus(callback func(PowerStatus)) (ResponseHandler, string) {
207207
return ResponseHandlerFunc(func(r protocol.Response) {
208+
if len(r.Data) == 0 {
209+
return
210+
}
208211
powerStatus := PowerStatus(r.Data[0])
209212
callback(powerStatus)
210213
}), "get_powerstat"
@@ -242,6 +245,9 @@ func (c *Conn) VFO(ctx context.Context) (VFO, error) {
242245
// OnVFO wraps the given callback function into the ResponseHandler interface and translates the generic response to a VFO value.
243246
func OnVFO(callback func(VFO)) (ResponseHandler, string) {
244247
return ResponseHandlerFunc(func(r protocol.Response) {
248+
if len(r.Data) == 0 {
249+
return
250+
}
245251
callback(VFO(r.Data[0]))
246252
}), "get_vfo"
247253
}
@@ -271,6 +277,9 @@ func (c *Conn) Frequency(ctx context.Context) (Frequency, error) {
271277
// OnFrequency wraps the given callback function into the ResponseHandler interface and translates the generic response to a Frequency value.
272278
func OnFrequency(callback func(Frequency)) (ResponseHandler, string) {
273279
return ResponseHandlerFunc(func(r protocol.Response) {
280+
if len(r.Data) == 0 {
281+
return
282+
}
274283
frequency, err := strconv.ParseFloat(r.Data[0], 64)
275284
if err != nil {
276285
log.Printf("hamlib: cannot parse frequency result: %v", err)
@@ -425,6 +434,9 @@ func (c *Conn) ModeAndPassband(ctx context.Context) (Mode, Frequency, error) {
425434
// OnModeAndPassband wraps the given callback function into the ResponseHandler interface and translates the generic response to mode and passband.
426435
func OnModeAndPassband(callback func(Mode, Frequency)) (ResponseHandler, string) {
427436
return ResponseHandlerFunc(func(r protocol.Response) {
437+
if len(r.Data) == 0 {
438+
return
439+
}
428440
mode := Mode(r.Data[0])
429441
passband, err := strconv.ParseFloat(r.Data[1], 64)
430442
if err != nil {
@@ -457,6 +469,9 @@ func (c *Conn) PowerLevel(ctx context.Context) (float64, error) {
457469
// OnPowerLevel wraps the given callback function into the ResponseHandler interface and translates the generic response to power level.
458470
func OnPowerLevel(callback func(float64)) (ResponseHandler, string, string) {
459471
return ResponseHandlerFunc(func(r protocol.Response) {
472+
if len(r.Data) == 0 {
473+
return
474+
}
460475
powerLevel, err := strconv.ParseFloat(r.Data[0], 64)
461476
if err != nil {
462477
log.Printf("hamlib: cannot parse power level result: %v", err)
@@ -497,6 +512,9 @@ func (c *Conn) PTT(ctx context.Context) (PTT, error) {
497512
// OnPTT wraps the given callback function into the ResponseHandler interface and translates the generic response to PTT state.
498513
func OnPTT(callback func(PTT)) (ResponseHandler, string) {
499514
return ResponseHandlerFunc(func(r protocol.Response) {
515+
if len(r.Data) == 0 {
516+
return
517+
}
500518
callback(PTT(r.Data[0]))
501519
}), "get_ptt"
502520
}
@@ -533,6 +551,9 @@ func (c *Conn) MorseSpeed(ctx context.Context) (float64, error) {
533551
// OnMorseSpeed wraps the given callback function into the ResponseHandler interface and translates the generic response to wpm.
534552
func OnMorseSpeed(callback func(int)) (ResponseHandler, string, string) {
535553
return ResponseHandlerFunc(func(r protocol.Response) {
554+
if len(r.Data) == 0 {
555+
return
556+
}
536557
wpm, err := strconv.Atoi(r.Data[0])
537558
if err != nil {
538559
log.Printf("hamlib: cannot parse morse speed result: %v", err)

0 commit comments

Comments
 (0)