Skip to content

Commit

Permalink
Improve traceback for case missing an attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
hmpf committed Apr 12, 2024
1 parent 9b473af commit d3d28c6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/zinolib/ritz.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,12 @@ def __getattr__(self, name):
elif "downtime" == name:
return self.get_downtime()
else:
self.__getattribute__(name)
# raise AttributeError("%s instance of type %s has no attribute '%s'" % (self.__class__, self._attrs["type"], name))
try:
self.__getattribute__(name)
except AttributeError as e:
msg = "%s of type %s has no attribute '%s'"
typename = self._attrs.get("type", "UNKNOWN")
raise AttributeError(msg % (self, typename, name))
return self

def __getitem__(self, key):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_ritz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest

from zinolib.ritz import Case


class CaseTest(unittest.TestCase):
class FakeZino:
def __init__(self, attrs=None):
if attrs:
self.attrs = attrs
else:
self.attrs = {}

def get_attributes(self, caseid):
return self.attrs

def test_golden_path_attribute_access(self):
zino = self.FakeZino({'foo': 'bar'})
case = Case(zino, 1)
self.assertTrue(case._attrs)
self.assertEqual(case.foo, 'bar')

def test_accessing_missing_attribute_should_fail_with_extra_info(self):
zino = self.FakeZino({'foo': 'bar'})
case = Case(zino, 1)
expected_msg = "<class 'zinolib.ritz.Case'>(1) of type UNKNOWN has no attribute 'xux'"
with self.assertRaises(AttributeError) as cm:
case.xux
self.assertEqual(cm.exception.args[0], expected_msg)

0 comments on commit d3d28c6

Please sign in to comment.