def _DISPATCH(self, body, ticket=None): """Dispatch message to the appropriate method in :attr:`state`, handle possible exceptions, and return a response suitable to be used in a reply. To protect from calling special methods it does not dispatch method names starting with underscore (``_``). This returns the return value or exception error with defaults fields in a suitable format to be used as a reply. The exceptions :exc:`SystemExit` and :exc:`KeyboardInterrupt` will not be handled, and will propagate. In the case of a successful call the return value will be:: {'ok': return_value, **default_fields} If the method raised an exception the return value will be:: {'nok': [repr exc, str traceback], **default_fields} :raises KeyError: if the method specified is unknown or is a special method (name starting with underscore). """ if ticket: sticket = '%s' % (shortuuid(ticket), ) else: ticket = sticket = str(next(self.ticket_counter)) try: method, args = itemgetter('method', 'args')(body) self.log.info('#%s --> %s', sticket, self._reprcall(method, args)) act = self.lookup_action(method) r = {'ok': act(args or {})} self.log.info('#%s <-- %s', sticket, reprkwargs(r)) except self.Next: raise except Exception as exc: einfo = sys.exc_info() r = {'nok': [safe_repr(exc), self._get_traceback(einfo)]} self.log.error('#%s <-- nok=%r', sticket, exc) return dict(self._default_fields, **r)
def test_reprkwargs(self): self.assertTrue(utils.reprkwargs({'foo': 'bar', 1: 2, 'k': 'v'}))
def test_reprkwargs(self): self.assertTrue(utils.reprkwargs({"foo": "bar", 1: 2, u"k": "v"}))