Skip to content

Commit

Permalink
Add coverage for callStorm as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
vEpiphyte committed Dec 31, 2024
1 parent 5667da7 commit 474a042
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions synapse/lib/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,15 @@ async def callStorm(self, text, opts=None):
extra={'synapse': {'text': text, 'username': user.name, 'user': user.iden}})
raise

except (s_stormctrl.StormLoopCtrl, s_stormctrl.StormGenrCtrl) as e:
if isinstance(e, s_stormctrl.StormLoopCtrl):
mesg = f'Loop control statement "{e.statement}" used outside of a loop.'
else:
mesg = f'Generator control statement "{e.statement}" used outside of a generator function.'
logmesg = f'Error during storm execution for {{ {text} }} - {mesg}'
logger.exception(logmesg, extra={'synapse': {'text': text, 'username': user.name, 'user': user.iden}})
raise s_exc.StormRuntimeError(mesg=mesg, statement=e.statement, highlight=e.get('highlight')) from e

except Exception:
logger.exception(f'Error during callStorm execution for {{ {text} }}',
extra={'synapse': {'text': text, 'username': user.name, 'user': user.iden}})
Expand Down
20 changes: 20 additions & 0 deletions synapse/tests/test_cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,26 @@ async def test_cortex_callstorm(self):
self.eq(cm.exception.get('hehe'), 'haha')
self.eq(cm.exception.get('key'), 1)

# We convert StormLoopCtrl and StormGenrCtrl into StormRuntimeError
opts = {'vars': {'i': 2}}
q = 'if ($i = 2) { break }'
with self.raises(s_exc.StormRuntimeError) as cm:
await core.callStorm(q, opts=opts)
self.eq(cm.exception.get('mesg'),
'Loop control statement "break" used outside of a loop.')

q = 'if ($i = 2) { continue }'
with self.raises(s_exc.StormRuntimeError) as cm:
await core.callStorm(q, opts=opts)
self.eq(cm.exception.get('mesg'),
'Loop control statement "continue" used outside of a loop.')

q = 'if ($i = 2) { stop }'
with self.raises(s_exc.StormRuntimeError) as cm:
await core.callStorm(q, opts=opts)
self.eq(cm.exception.get('mesg'),
'Generator control statement "stop" used outside of a generator function.')

with self.getAsyncLoggerStream('synapse.lib.view', 'callStorm cancelled') as stream:
async with core.getLocalProxy() as proxy:

Expand Down

0 comments on commit 474a042

Please sign in to comment.