mirror of
https://github.com/lowobservable/oec.git
synced 2026-03-06 03:18:54 +00:00
Fix aggregate execution error message
This commit is contained in:
@@ -13,10 +13,15 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class ExecuteError(Exception):
|
||||
def __init__(self, errors, responses):
|
||||
if len(errors) == 1:
|
||||
message = str(errors[0])
|
||||
else:
|
||||
message = f'{len(errors)} occurred'
|
||||
if not errors:
|
||||
raise ValueError('Errors required')
|
||||
|
||||
message = str(errors[0]) or repr(errors[0])
|
||||
|
||||
if len(errors) == 2:
|
||||
message += ' and 1 other error'
|
||||
elif len(errors) > 2:
|
||||
message += f' and {len(errors) - 1} other errors'
|
||||
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from coax import ReadAddressCounterHi, ReadAddressCounterLo, ProtocolError
|
||||
from coax import ReadAddressCounterHi, ReadAddressCounterLo, ProtocolError, ReceiveTimeout, ReceiveError
|
||||
|
||||
import context
|
||||
|
||||
@@ -105,3 +105,24 @@ class InterfaceWrapperExecuteTestCase(unittest.TestCase):
|
||||
self.assertIsInstance(error.errors[0], ProtocolError)
|
||||
|
||||
self.assertEqual(len(error.responses), 2)
|
||||
|
||||
class ExecuteErrorTestCase(unittest.TestCase):
|
||||
def test_single_str_error(self):
|
||||
error = ExecuteError([ReceiveError('ReceiveErrorMessage')], [])
|
||||
|
||||
self.assertEqual(str(error), 'ReceiveErrorMessage')
|
||||
|
||||
def test_single_repr_error(self):
|
||||
error = ExecuteError([ReceiveTimeout()], [])
|
||||
|
||||
self.assertEqual(str(error), 'ReceiveTimeout()')
|
||||
|
||||
def test_multiple_errors_first_str_error(self):
|
||||
error = ExecuteError([ReceiveError('ReceiveErrorMessage'), ReceiveTimeout()], [])
|
||||
|
||||
self.assertEqual(str(error), 'ReceiveErrorMessage and 1 other error')
|
||||
|
||||
def test_multiple_errors_first_repr_error(self):
|
||||
error = ExecuteError([ReceiveTimeout(), ReceiveError('ReceiveErrorMessage')], [])
|
||||
|
||||
self.assertEqual(str(error), 'ReceiveTimeout() and 1 other error')
|
||||
|
||||
Reference in New Issue
Block a user