async def _onTaskInit(self, link, mesg): task = mesg[1].get('task') name = mesg[1].get('name') sess = link.get('sess') if sess is None: raise s_exc.NoSuchObj(name=name) item = sess.getSessItem(name) if item is None: raise s_exc.NoSuchObj(name=name) try: methname, args, kwargs = mesg[1].get('todo') if methname[0] == '_': raise s_exc.NoSuchMeth(name=methname) meth = getattr(item, methname, None) if meth is None: logger.warning('%r has no method: %s', item, methname) raise s_exc.NoSuchMeth(name=methname) valu = await self._runTodoMeth(link, meth, args, kwargs) mesg = self._getTaskFiniMesg(task, valu) await link.tx(mesg) # if it's a Share(), spin off the share loop if isinstance(valu, s_share.Share): if isinstance(item, s_base.Base): item.onfini(valu) async def spinshareloop(): try: await valu._runShareLoop() except asyncio.CancelledError: pass except Exception: logger.exception('Error running %r', valu) finally: await valu.fini() self.schedCoro(spinshareloop()) except (asyncio.CancelledError, Exception) as e: logger.exception('on task:init: %r', mesg) retn = s_common.retnexc(e) await link.tx( ('task:fini', {'task': task, 'retn': retn}) )
async def _onTaskV2Init(self, link, mesg): # t2:init is used by the pool sockets on the client name = mesg[1].get('name') sidn = mesg[1].get('sess') todo = mesg[1].get('todo') try: if sidn is None or todo is None: raise s_exc.NoSuchObj(name=name) sess = self.sessions.get(sidn) if sess is None: raise s_exc.NoSuchObj(name=name) item = sess.getSessItem(name) if item is None: raise s_exc.NoSuchObj(name=name) s_scope.set('sess', sess) s_scope.set('link', link) methname, args, kwargs = todo if methname[0] == '_': raise s_exc.NoSuchMeth(name=methname) meth = getattr(item, methname, None) if meth is None: logger.warning('%r has no method: %r', item, methname) raise s_exc.NoSuchMeth(name=methname) sessitem = await t2call(link, meth, args, kwargs) if sessitem is not None: sess.onfini(sessitem) except Exception as e: logger.exception('on t2:init: %r' % (mesg, )) if not link.isfini: retn = s_common.retnexc(e) await link.tx(('t2:fini', {'retn': retn}))
async def test_base_exc(self): logs = [] async with await s_base.Base.anit() as base: base.on('log', logs.append) try: raise s_exc.NoSuchObj(name='hehe') except Exception as e: await base.exc(e) mesg = logs[0] self.eq(mesg[1].get('err'), 'NoSuchObj')
def test_eventbus_exc(self): logs = [] with s_eventbus.EventBus() as ebus: ebus.on('log', logs.append) try: raise s_exc.NoSuchObj(name='hehe') except Exception as e: ebus.exc(e) mesg = logs[0] self.eq(mesg[1].get('err'), 'NoSuchObj')
async def _onTaskV2Init(self, link, mesg): # t2:init is used by the pool sockets on the client name = mesg[1].get('name') sidn = mesg[1].get('sess') todo = mesg[1].get('todo') try: if sidn is None or todo is None: raise s_exc.NoSuchObj(name=name) sess = self.sessions.get(sidn) if sess is None: raise s_exc.NoSuchObj(name=name) item = sess.getSessItem(name) if item is None: raise s_exc.NoSuchObj(name=name) s_scope.set('sess', sess) # TODO set user.... methname, args, kwargs = todo if methname[0] == '_': raise s_exc.NoSuchMeth(name=methname) meth = getattr(item, methname, None) if meth is None: logger.warning(f'{item!r} has no method: {methname}') raise s_exc.NoSuchMeth(name=methname) valu = meth(*args, **kwargs) if s_coro.iscoro(valu): valu = await valu if isinstance(valu, types.AsyncGeneratorType): try: await link.tx(('t2:genr', {})) async for item in valu: await link.tx(('t2:yield', {'retn': (True, item)})) await link.tx(('t2:yield', {'retn': None})) except Exception as e: if not link.isfini: retn = s_common.retnexc(e) await link.tx(('t2:yield', {'retn': retn})) return if isinstance(valu, types.GeneratorType): try: await link.tx(('t2:genr', {})) for item in valu: await link.tx(('t2:yield', {'retn': (True, item)})) await link.tx(('t2:yield', {'retn': None})) except Exception as e: if not link.isfini: retn = s_common.retnexc(e) await link.tx(('t2:yield', {'retn': (False, retn)})) return if isinstance(valu, s_share.Share): iden = s_common.guid() sess.setSessItem(iden, valu) await link.tx(('t2:share', {'iden': iden})) return await link.tx(('t2:fini', {'retn': (True, valu)})) except Exception as e: logger.exception('on task:init: %r' % (mesg, )) if not link.isfini: retn = s_common.retnexc(e) await link.tx(('t2:fini', {'retn': retn}))