Skip to content

Commit 87665c9

Browse files
committed
Better error messages for discoveries and monitor
1 parent 2fabff9 commit 87665c9

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

arduino/discovery/discovery.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,10 @@ func (disc *PluggableDiscovery) Run() (err error) {
312312
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "HELLO", err)
313313
} else if msg.EventType != "hello" {
314314
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "hello", msg.EventType)
315-
} else if strings.ToUpper(msg.Message) != "OK" || msg.Error {
315+
} else if msg.Error {
316316
return errors.Errorf(tr("command failed: %s"), msg.Message)
317+
} else if strings.ToUpper(msg.Message) != "OK" {
318+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
317319
} else if msg.ProtocolVersion > 1 {
318320
return errors.Errorf(tr("protocol version not supported: requested 1, got %d"), msg.ProtocolVersion)
319321
}
@@ -333,8 +335,10 @@ func (disc *PluggableDiscovery) Start() error {
333335
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "START", err)
334336
} else if msg.EventType != "start" {
335337
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start", msg.EventType)
336-
} else if strings.ToUpper(msg.Message) != "OK" || msg.Error {
338+
} else if msg.Error {
337339
return errors.Errorf(tr("command failed: %s"), msg.Message)
340+
} else if strings.ToUpper(msg.Message) != "OK" {
341+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
338342
}
339343
disc.statusMutex.Lock()
340344
defer disc.statusMutex.Unlock()
@@ -353,8 +357,10 @@ func (disc *PluggableDiscovery) Stop() error {
353357
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "STOP", err)
354358
} else if msg.EventType != "stop" {
355359
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "stop", msg.EventType)
356-
} else if strings.ToUpper(msg.Message) != "OK" || msg.Error {
360+
} else if msg.Error {
357361
return errors.Errorf(tr("command failed: %s"), msg.Message)
362+
} else if strings.ToUpper(msg.Message) != "OK" {
363+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
358364
}
359365
disc.statusMutex.Lock()
360366
defer disc.statusMutex.Unlock()
@@ -376,8 +382,10 @@ func (disc *PluggableDiscovery) Quit() error {
376382
return fmt.Errorf(tr("calling %[1]s: %[2]w"), "QUIT", err)
377383
} else if msg.EventType != "quit" {
378384
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "quit", msg.EventType)
379-
} else if strings.ToUpper(msg.Message) != "OK" || msg.Error {
385+
} else if msg.Error {
380386
return errors.Errorf(tr("command failed: %s"), msg.Message)
387+
} else if strings.ToUpper(msg.Message) != "OK" {
388+
return errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
381389
}
382390
disc.killProcess()
383391
return nil
@@ -416,8 +424,10 @@ func (disc *PluggableDiscovery) StartSync(size int) (<-chan *Event, error) {
416424
return nil, fmt.Errorf(tr("calling %[1]s: %[2]w"), "START_SYNC", err)
417425
} else if msg.EventType != "start_sync" {
418426
return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "start_sync", msg.EventType)
419-
} else if strings.ToUpper(msg.Message) != "OK" || msg.Error {
427+
} else if msg.Error {
420428
return nil, errors.Errorf(tr("command failed: %s"), msg.Message)
429+
} else if strings.ToUpper(msg.Message) != "OK" {
430+
return nil, errors.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
421431
}
422432

423433
disc.statusMutex.Lock()

arduino/monitor/monitor.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,12 @@ func (mon *PluggableMonitor) waitMessage(timeout time.Duration, expectedEvt stri
145145
if msg.EventType != expectedEvt {
146146
return msg, fmt.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), expectedEvt, msg.EventType)
147147
}
148-
if strings.ToUpper(msg.Message) != "OK" || msg.Error {
148+
if msg.Error {
149149
return msg, fmt.Errorf(tr("command '%[1]s' failed: %[2]s"), expectedEvt, msg.Message)
150150
}
151+
if strings.ToUpper(msg.Message) != "OK" {
152+
return msg, fmt.Errorf(tr("communication out of sync, expected '%[1]s', received '%[2]s'"), "OK", msg.Message)
153+
}
151154
return msg, nil
152155
}
153156

0 commit comments

Comments
 (0)