def _get_response(self, ctx, proxy, topic, data): """Process a curried message and cast the result to topic.""" LOG.debug(_("Running func with context: %s"), ctx.to_dict()) data.setdefault('version', None) data.setdefault('args', {}) try: result = proxy.dispatch(ctx, data['version'], data['method'], data.get('namespace'), **data['args']) return ConsumerBase.normalize_reply(result, ctx.replies) except greenlet.GreenletExit: # ignore these since they are just from shutdowns pass except rpc_common.ClientException as e: LOG.debug( _("Expected exception during message handling (%s)") % e._exc_info[1]) return { 'exc': rpc_common.serialize_remote_exception(e._exc_info, log_failure=False) } except Exception: LOG.error(_("Exception during message handling")) return { 'exc': rpc_common.serialize_remote_exception(sys.exc_info()) }
def test_serialize_remote_exception_cell_hop(self): # A remote remote (no typo) exception should maintain its type and # module, when being re-serialized, so that through any amount of cell # hops up, it can pop out with the right type expected = { 'class': 'OpenstackException', 'module': 'openstack.common.exception', 'message': exception.OpenstackException.msg_fmt, } def raise_remote_exception(): try: raise exception.OpenstackException() except Exception as e: ex_type = type(e) message = str(e) str_override = lambda self: message new_ex_type = type(ex_type.__name__ + "_Remote", (ex_type,), {'__str__': str_override, '__unicode__': str_override}) new_ex_type.__module__ = '%s_Remote' % e.__class__.__module__ e.__class__ = new_ex_type raise try: raise_remote_exception() except Exception: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) self.assertEqual(expected['message'], failure['message'])
def msg_reply(conf, msg_id, reply_q, connection_pool, reply=None, failure=None, ending=False, log_failure=True): """Sends a reply or an error on the channel signified by msg_id. Failure should be a sys.exc_info() tuple. """ with ConnectionContext(conf, connection_pool) as conn: if failure: failure = rpc_common.serialize_remote_exception( failure, log_failure) msg = {'result': reply, 'failure': failure} if ending: msg['ending'] = True _add_unique_id(msg) # If a reply_q exists, add the msg_id to the reply and pass the # reply_q to direct_send() to use it as the response queue. # Otherwise use the msg_id for backward compatibilty. if reply_q: msg['_msg_id'] = msg_id conn.direct_send(reply_q, rpc_common.serialize_msg(msg)) else: conn.direct_send(msg_id, rpc_common.serialize_msg(msg))
def msg_reply(conf, msg_id, connection_pool, reply=None, failure=None, ending=False): """Sends a reply or an error on the channel signified by msg_id. Failure should be a sys.exc_info() tuple. """ with ConnectionContext(conf, connection_pool) as conn: if failure: failure = rpc_common.serialize_remote_exception(failure) try: msg = {'result': reply, 'failure': failure} except TypeError: msg = { 'result': dict( (k, repr(v)) for k, v in reply.__dict__.iteritems()), 'failure': failure } if ending: msg['ending'] = True conn.direct_send(msg_id, msg)
def test_serialize_remote_exception_cell_hop(self): # A remote remote (no typo) exception should maintain its type and # module, when being re-serialized, so that through any amount of cell # hops up, it can pop out with the right type expected = { 'class': 'FakeUserDefinedException', 'module': self.__class__.__module__, 'message': 'foobar', } def raise_remote_exception(): try: raise FakeUserDefinedException('foobar') except Exception as e: ex_type = type(e) message = str(e) str_override = lambda self: message new_ex_type = type(ex_type.__name__ + "_Remote", (ex_type, ), { '__str__': str_override, '__unicode__': str_override }) new_ex_type.__module__ = '%s_Remote' % e.__class__.__module__ e.__class__ = new_ex_type raise try: raise_remote_exception() except Exception: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) self.assertEqual(expected['message'], failure['message'])
def msg_reply(conf, msg_id, reply_q, connection_pool, reply=None, failure=None, ending=False, log_failure=True): """Sends a reply or an error on the channel signified by msg_id. Failure should be a sys.exc_info() tuple. """ with ConnectionContext(conf, connection_pool) as conn: if failure: failure = rpc_common.serialize_remote_exception(failure, log_failure) try: msg = {'result': reply, 'failure': failure} except TypeError: msg = {'result': dict((k, repr(v)) for k, v in reply.__dict__.iteritems()), 'failure': failure} if ending: msg['ending'] = True _add_unique_id(msg) # If a reply_q exists, add the msg_id to the reply and pass the # reply_q to direct_send() to use it as the response queue. # Otherwise use the msg_id for backward compatibilty. if reply_q: msg['_msg_id'] = msg_id conn.direct_send(reply_q, rpc_common.serialize_msg(msg)) else: conn.direct_send(msg_id, rpc_common.serialize_msg(msg))
def test_serialize_remote_exception(self): expected = {"class": "Exception", "module": "exceptions", "message": "test"} try: raise_exception() except Exception as exc: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) # assure the traceback was added self.assertEqual(expected["class"], failure["class"]) self.assertEqual(expected["module"], failure["module"]) self.assertEqual(expected["message"], failure["message"])
def _get_response(self, ctx, proxy, topic, data): """Process a curried message and cast the result to topic.""" LOG.debug(_("Running func with context: %s"), ctx.to_dict()) data.setdefault("version", None) data.setdefault("args", []) try: result = proxy.dispatch(ctx, data["version"], data["method"], **data["args"]) return ConsumerBase.normalize_reply(result, ctx.replies) except greenlet.GreenletExit: # ignore these since they are just from shutdowns pass except Exception: return {"exc": rpc_common.serialize_remote_exception(sys.exc_info())}
def _get_response(self, ctx, proxy, topic, data): """Process a curried message and cast the result to topic.""" LOG.debug(_("Running func with context: %s"), ctx.to_dict()) data.setdefault('version', None) data.setdefault('args', {}) try: result = proxy.dispatch( ctx, data['version'], data['method'], data.get('namespace'), **data['args']) return ConsumerBase.normalize_reply(result, ctx.replies) except greenlet.GreenletExit: # ignore these since they are just from shutdowns pass except rpc_common.ClientException as e: LOG.debug(_("Expected exception during message handling (%s)") % e._exc_info[1]) return {'exc': rpc_common.serialize_remote_exception(e._exc_info, log_failure=False)} except Exception: LOG.error(_("Exception during message handling")) return {'exc': rpc_common.serialize_remote_exception(sys.exc_info())}
def test_serialize_remote_custom_exception(self): expected = { 'class': 'FakeUserDefinedException', 'module': self.__class__.__module__, 'message': 'test', } try: raise FakeUserDefinedException('test') except Exception: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) self.assertEqual(expected['message'], failure['message'])
def test_serialize_remote_exception(self): expected = { 'class': 'Exception', 'module': 'exceptions', 'message': 'test', } try: raise_exception() except Exception: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) self.assertEqual(expected['message'], failure['message'])
def test_serialize_remote_exception(self): expected = { 'class': 'Exception', 'module': 'exceptions', 'message': 'test', } try: raise Exception("test") except Exception: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) self.assertEqual(expected['message'], failure['message'])
def _get_response(self, ctx, proxy, topic, data): """Process a curried message and cast the result to topic.""" LOG.debug(_("Running func with context: %s"), ctx.to_dict()) data.setdefault('version', None) data.setdefault('args', []) try: result = proxy.dispatch(ctx, data['version'], data['method'], **data['args']) return ConsumerBase.normalize_reply(result, ctx.replies) except greenlet.GreenletExit: # ignore these since they are just from shutdowns pass except Exception: return { 'exc': rpc_common.serialize_remote_exception(sys.exc_info()) }
def test_serialize_remote_exception(self): expected = { 'class': 'Exception', 'module': 'exceptions', 'message': 'test', } try: raise_exception() except Exception as exc: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) #assure the traceback was added self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) self.assertEqual(expected['message'], failure['message'])
def test_serialize_remote_custom_exception(self): def raise_custom_exception(): raise exception.MalformedRequestBody(reason='test') expected = { 'class': 'MalformedRequestBody', 'module': 'openstack.common.exception', 'message': str(exception.MalformedRequestBody(reason='test')), } try: raise_custom_exception() except Exception: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) self.assertEqual(expected['message'], failure['message'])
def test_serialize_remote_custom_exception(self): def raise_custom_exception(): raise exception.OpenstackException() expected = { 'class': 'OpenstackException', 'module': 'openstack.common.exception', 'message': exception.OpenstackException.message, } try: raise_custom_exception() except Exception as exc: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) #assure the traceback was added self.assertEqual(expected['class'], failure['class']) self.assertEqual(expected['module'], failure['module']) self.assertEqual(expected['message'], failure['message'])
def msg_reply(conf, msg_id, connection_pool, reply=None, failure=None, ending=False): """Sends a reply or an error on the channel signified by msg_id. Failure should be a sys.exc_info() tuple. """ with ConnectionContext(conf, connection_pool) as conn: if failure: failure = rpc_common.serialize_remote_exception(failure) try: msg = {'result': reply, 'failure': failure} except TypeError: msg = {'result': dict((k, repr(v)) for k, v in reply.__dict__.iteritems()), 'failure': failure} if ending: msg['ending'] = True conn.direct_send(msg_id, msg)
def test_serialize_remote_custom_exception(self): def raise_custom_exception(): raise exception.OpenstackException() expected = { "class": "OpenstackException", "module": "openstack.common.exception", "message": exception.OpenstackException.message, } try: raise_custom_exception() except Exception as exc: failure = rpc_common.serialize_remote_exception(sys.exc_info()) failure = jsonutils.loads(failure) # assure the traceback was added self.assertEqual(expected["class"], failure["class"]) self.assertEqual(expected["module"], failure["module"]) self.assertEqual(expected["message"], failure["message"])