From 37518963aa908b56fb409610bb32f97779da64f8 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 24 Jan 2022 11:41:21 +0100 Subject: [PATCH 1/3] Allow case-insensitive 'ok'/'OK' replies from discoveries and monitors --- arduino/discovery/discovery.go | 10 +++++----- arduino/monitor/monitor.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arduino/discovery/discovery.go b/arduino/discovery/discovery.go index 78956a63331..3fdbe0c1a4b 100644 --- a/arduino/discovery/discovery.go +++ b/arduino/discovery/discovery.go @@ -312,7 +312,7 @@ func (disc *PluggableDiscovery) Run() (err error) { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "HELLO", err) } else if msg.EventType != "hello" { return errors.Errorf(tr("communication out of sync, expected 'hello', received '%s'"), msg.EventType) - } else if msg.Message != "OK" || msg.Error { + } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) } else if msg.ProtocolVersion > 1 { return errors.Errorf(tr("protocol version not supported: requested 1, got %d"), msg.ProtocolVersion) @@ -333,7 +333,7 @@ func (disc *PluggableDiscovery) Start() error { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "START", err) } else if msg.EventType != "start" { return errors.Errorf(tr("communication out of sync, expected 'start', received '%s'"), msg.EventType) - } else if msg.Message != "OK" || msg.Error { + } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) } disc.statusMutex.Lock() @@ -353,7 +353,7 @@ func (disc *PluggableDiscovery) Stop() error { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "STOP", err) } else if msg.EventType != "stop" { return errors.Errorf(tr("communication out of sync, expected 'stop', received '%s'"), msg.EventType) - } else if msg.Message != "OK" || msg.Error { + } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) } disc.statusMutex.Lock() @@ -376,7 +376,7 @@ func (disc *PluggableDiscovery) Quit() error { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "QUIT", err) } else if msg.EventType != "quit" { return errors.Errorf(tr("communication out of sync, expected 'quit', received '%s'"), msg.EventType) - } else if msg.Message != "OK" || msg.Error { + } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) } disc.killProcess() @@ -416,7 +416,7 @@ func (disc *PluggableDiscovery) StartSync(size int) (<-chan *Event, error) { return nil, fmt.Errorf(tr("calling %[1]s: %[2]w"), "START_SYNC", err) } else if msg.EventType != "start_sync" { return nil, errors.Errorf(tr("communication out of sync, expected 'start_sync', received '%s'"), msg.EventType) - } else if msg.Message != "OK" || msg.Error { + } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return nil, errors.Errorf(tr("command failed: %s"), msg.Message) } diff --git a/arduino/monitor/monitor.go b/arduino/monitor/monitor.go index 76e36b6223f..72631e570f7 100644 --- a/arduino/monitor/monitor.go +++ b/arduino/monitor/monitor.go @@ -145,7 +145,7 @@ func (mon *PluggableMonitor) waitMessage(timeout time.Duration, expectedEvt stri if msg.EventType != expectedEvt { return msg, fmt.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), expectedEvt, msg.EventType) } - if msg.Message != "OK" || msg.Error { + if strings.ToUpper(msg.Message) != "OK" || msg.Error { return msg, fmt.Errorf(tr("command '%[1]s' failed: %[2]s"), expectedEvt, msg.Message) } return msg, nil From 2fabff9ea2479cb5c8c32ea10d47cd6b8b3a916f Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 24 Jan 2022 13:56:17 +0100 Subject: [PATCH 2/3] Factor the same messages to reduce translations strings --- arduino/discovery/discovery.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/arduino/discovery/discovery.go b/arduino/discovery/discovery.go index 3fdbe0c1a4b..6566dca25e8 100644 --- a/arduino/discovery/discovery.go +++ b/arduino/discovery/discovery.go @@ -311,7 +311,7 @@ func (disc *PluggableDiscovery) Run() (err error) { if msg, err := disc.waitMessage(time.Second * 10); err != nil { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "HELLO", err) } else if msg.EventType != "hello" { - return errors.Errorf(tr("communication out of sync, expected 'hello', received '%s'"), msg.EventType) + return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "hello", msg.EventType) } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) } else if msg.ProtocolVersion > 1 { @@ -332,7 +332,7 @@ func (disc *PluggableDiscovery) Start() error { if msg, err := disc.waitMessage(time.Second * 10); err != nil { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "START", err) } else if msg.EventType != "start" { - return errors.Errorf(tr("communication out of sync, expected 'start', received '%s'"), msg.EventType) + return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start", msg.EventType) } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) } @@ -352,7 +352,7 @@ func (disc *PluggableDiscovery) Stop() error { if msg, err := disc.waitMessage(time.Second * 10); err != nil { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "STOP", err) } else if msg.EventType != "stop" { - return errors.Errorf(tr("communication out of sync, expected 'stop', received '%s'"), msg.EventType) + return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "stop", msg.EventType) } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) } @@ -375,7 +375,7 @@ func (disc *PluggableDiscovery) Quit() error { if msg, err := disc.waitMessage(time.Second * 10); err != nil { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "QUIT", err) } else if msg.EventType != "quit" { - return errors.Errorf(tr("communication out of sync, expected 'quit', received '%s'"), msg.EventType) + return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "quit", msg.EventType) } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) } @@ -392,7 +392,7 @@ func (disc *PluggableDiscovery) List() ([]*Port, error) { if msg, err := disc.waitMessage(time.Second * 10); err != nil { return nil, fmt.Errorf(tr("calling %[1]s: %[2]w"), "LIST", err) } else if msg.EventType != "list" { - return nil, errors.Errorf(tr("communication out of sync, expected 'list', received '%s'"), msg.EventType) + return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "list", msg.EventType) } else if msg.Error { return nil, errors.Errorf(tr("command failed: %s"), msg.Message) } else { @@ -415,7 +415,7 @@ func (disc *PluggableDiscovery) StartSync(size int) (<-chan *Event, error) { if msg, err := disc.waitMessage(time.Second * 10); err != nil { return nil, fmt.Errorf(tr("calling %[1]s: %[2]w"), "START_SYNC", err) } else if msg.EventType != "start_sync" { - return nil, errors.Errorf(tr("communication out of sync, expected 'start_sync', received '%s'"), msg.EventType) + return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start_sync", msg.EventType) } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { return nil, errors.Errorf(tr("command failed: %s"), msg.Message) } From 87665c9d91216011f768725d118720ce6eb02d50 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 24 Jan 2022 14:01:21 +0100 Subject: [PATCH 3/3] Better error messages for discoveries and monitor --- arduino/discovery/discovery.go | 20 +++++++++++++++----- arduino/monitor/monitor.go | 5 ++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/arduino/discovery/discovery.go b/arduino/discovery/discovery.go index 6566dca25e8..648c536733d 100644 --- a/arduino/discovery/discovery.go +++ b/arduino/discovery/discovery.go @@ -312,8 +312,10 @@ func (disc *PluggableDiscovery) Run() (err error) { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "HELLO", err) } else if msg.EventType != "hello" { return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "hello", msg.EventType) - } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { + } else if msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) + } else if strings.ToUpper(msg.Message) != "OK" { + return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message) } else if msg.ProtocolVersion > 1 { return errors.Errorf(tr("protocol version not supported: requested 1, got %d"), msg.ProtocolVersion) } @@ -333,8 +335,10 @@ func (disc *PluggableDiscovery) Start() error { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "START", err) } else if msg.EventType != "start" { return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start", msg.EventType) - } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { + } else if msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) + } else if strings.ToUpper(msg.Message) != "OK" { + return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message) } disc.statusMutex.Lock() defer disc.statusMutex.Unlock() @@ -353,8 +357,10 @@ func (disc *PluggableDiscovery) Stop() error { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "STOP", err) } else if msg.EventType != "stop" { return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "stop", msg.EventType) - } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { + } else if msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) + } else if strings.ToUpper(msg.Message) != "OK" { + return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message) } disc.statusMutex.Lock() defer disc.statusMutex.Unlock() @@ -376,8 +382,10 @@ func (disc *PluggableDiscovery) Quit() error { return fmt.Errorf(tr("calling %[1]s: %[2]w"), "QUIT", err) } else if msg.EventType != "quit" { return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "quit", msg.EventType) - } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { + } else if msg.Error { return errors.Errorf(tr("command failed: %s"), msg.Message) + } else if strings.ToUpper(msg.Message) != "OK" { + return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message) } disc.killProcess() return nil @@ -416,8 +424,10 @@ func (disc *PluggableDiscovery) StartSync(size int) (<-chan *Event, error) { return nil, fmt.Errorf(tr("calling %[1]s: %[2]w"), "START_SYNC", err) } else if msg.EventType != "start_sync" { return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start_sync", msg.EventType) - } else if strings.ToUpper(msg.Message) != "OK" || msg.Error { + } else if msg.Error { return nil, errors.Errorf(tr("command failed: %s"), msg.Message) + } else if strings.ToUpper(msg.Message) != "OK" { + return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message) } disc.statusMutex.Lock() diff --git a/arduino/monitor/monitor.go b/arduino/monitor/monitor.go index 72631e570f7..09db6b2bdbf 100644 --- a/arduino/monitor/monitor.go +++ b/arduino/monitor/monitor.go @@ -145,9 +145,12 @@ func (mon *PluggableMonitor) waitMessage(timeout time.Duration, expectedEvt stri if msg.EventType != expectedEvt { return msg, fmt.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), expectedEvt, msg.EventType) } - if strings.ToUpper(msg.Message) != "OK" || msg.Error { + if msg.Error { return msg, fmt.Errorf(tr("command '%[1]s' failed: %[2]s"), expectedEvt, msg.Message) } + if strings.ToUpper(msg.Message) != "OK" { + return msg, fmt.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message) + } return msg, nil }