[DX] adding error messages to {ResultSet,ActionsRegistry}.possible_actions 3.26
authorLaurent Peuch <cortex@worlddomination.be>
Thu, 14 Mar 2019 09:38:20 +0100
branch3.26
changeset 12497 c81e29cd8cff
parent 12493 2c47461fec21
child 12510 7f4411168ed0
[DX] adding error messages to {ResultSet,ActionsRegistry}.possible_actions Try to make it more accessible, when we first encountered these errors it it didn't made any sense to us.
cubicweb/cwvreg.py
cubicweb/rset.py
--- a/cubicweb/cwvreg.py	Wed Mar 13 14:01:10 2019 +0100
+++ b/cubicweb/cwvreg.py	Thu Mar 14 09:38:20 2019 +0100
@@ -250,6 +250,15 @@
                       key=lambda x: x.order)
 
     def possible_actions(self, req, rset=None, **kwargs):
+        if not kwargs:
+            raise ValueError("ActionsRegistry.possible_actions should always be "
+                             "called with additional keywords arguments (in "
+                             "addition of req and the optional argument rset) "
+                             "to optimised cache computation but you provided "
+                             "none.\n\nFor exemple if you are working on a view, "
+                             "provide the view as a keyword argument to "
+                             "possible_actions like this: view=my_view.")
+
         if rset is None:
             actions = self.poss_visible_objects(req, rset=rset, **kwargs)
         else:
--- a/cubicweb/rset.py	Wed Mar 13 14:01:10 2019 +0100
+++ b/cubicweb/rset.py	Thu Mar 14 09:38:20 2019 +0100
@@ -103,6 +103,11 @@
         """Return possible actions on this result set. Should always be called with the same
         arguments so it may be computed only once.
         """
+        if not kwargs:
+            raise ValueError("ResultSet.possible_actions is expecting to receive "
+                             "keywords arguments to be able to compute access to "
+                             "the cache only once but you provided none.")
+
         key = tuple(sorted(kwargs.items()))
         if self._actions_cache is None:
             actions = self.req.vreg['actions'].poss_visible_objects(
@@ -110,9 +115,12 @@
             self._actions_cache = (key, actions)
             return actions
         else:
-            assert key == self._actions_cache[0], \
-                'unexpected new arguments for possible actions (%s vs %s)' % (
-                    key, self._actions_cache[0])
+            if key != self._actions_cache[0]:
+                raise ValueError("ResultSet.possible_actions expects to always "
+                                 "receive the same arguments to compute the "
+                                 "cache once, but you've call it with the "
+                                 "arguments: '%s' that aren't the same as the "
+                                 "previously used combinaison '%s'" % (key, self._actions_cache[0]))
             return self._actions_cache[1]
 
     def __len__(self):