From c93d9db530fead50f44b3ae1e7b5f95c20683575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Mon, 4 Mar 2024 22:09:48 +0100 Subject: [PATCH] GetName: Better error reporting --- get_name.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/get_name.go b/get_name.go index 361a890..f236795 100644 --- a/get_name.go +++ b/get_name.go @@ -1,7 +1,9 @@ package satel import ( + "encoding/hex" "errors" + "fmt" "strings" "golang.org/x/text/encoding/charmap" @@ -33,7 +35,7 @@ type NameEvent struct { func makeNameEvent(bytes []byte) (*NameEvent, error) { if len(bytes) < 19 { - return nil, errors.New("Received data too short") + return nil, errors.New(fmt.Sprint("Received data too short: ", hex.Dump(bytes))) } cp1250dec := charmap.Windows1250.NewDecoder() name, err := cp1250dec.String(string(bytes[3:19])) @@ -60,21 +62,23 @@ func (s *Satel) GetName(devType DeviceType, devIndex byte) (*NameEvent, error) { err := s.sendCmd(0xEE, byte(devType), devIndex) if err != nil { resultChan <- getNameResult{nil, errors.New("Could not send Satel read device name")} + return } var bytes = <-s.rawEvents - if len(bytes) < (1 + 19 + 2) { - resultChan <- getNameResult{nil, errors.New("Received data too short")} + if len(bytes) < (1 + 2) { + resultChan <- getNameResult{nil, errors.New(fmt.Sprint("Received data too short to get name: ", hex.Dump(bytes)))} + return } cmd := bytes[0] bytes = bytes[1 : len(bytes)-2] if cmd == 0xEF { - resultChan <- getNameResult{nil, errors.New("Did not receive a name")} + resultChan <- getNameResult{nil, errors.New(fmt.Sprint("Received an error instead of a name: ", hex.Dump(bytes)))} } else if cmd == 0xEE { name, err := makeNameEvent(bytes) resultChan <- getNameResult{name, err} } else { - resultChan <- getNameResult{nil, errors.New("Received unexpected result instead of a name")} + resultChan <- getNameResult{nil, errors.New(fmt.Sprint("Received unexpected reply: ", hex.Dump(bytes)))} } } nameResult := <-resultChan