def test_exception_trace(magic, patch, story, with_root): root_message = 'test' root = BaseException(root_message) if not with_root: root = None root_message = '' patch.object(story, 'get_stack', return_value=['1', '2', '3']) patch.object(story, 'line', side_effect=[{ 'src': 'line_1' }, { 'method': 'hello' }, { 'src': 'line_3' }]) story.name = 'story_name' ex = StoryscriptError(message='unknown error', story=story, line=magic(), root=root) # We cache the result of str(ex) because if we don't, __str__ will run # again, and will then throw a StopIteration exception since # pytest mocks the function `line` above. str_version = str(ex) if root_message: root_message = f': {root_message}' assert str_version == f"""An exception has occurred:
async def execute_block_proxy(*args): line = args[2] method = line['method'] # throw an exception inside the try block if method == 'try' or method == 'catch' and should_throw_exc: raise StoryscriptError() else: assert method == 'finally' or method == 'catch' return await execute_block(*args)
def foo(*args, **kwargs): raise StoryscriptError()
sentry_agent = SentryAgent(dsn='sentry_dsn', release='release', logger=logger) sentry_sdk.init.assert_called_with(dsn='sentry_dsn', release='release', max_breadcrumbs=0, integrations=[], default_integrations=False) assert sentry_agent._logger == logger assert sentry_agent._release == 'release' @mark.parametrize('ex', [Exception(), StoryscriptError()]) @mark.asyncio async def test_capture(patch, magic, async_mock, ex): logger = magic() patch.object(sentry_sdk, 'init') patch.object(sentry_sdk, 'capture_exception') patch.object(Scope, 'clear') patch.object(Scope, '__setattr__') sentry_agent = SentryAgent(dsn='sentry_dsn', release='release', logger=logger) expected_story_line = '28' expected_story_name = 'story_name'
def exc(*args): raise StoryscriptError()
assert handler.logger == logger def test_finished(magic, logger): handler = BaseHandler(magic(), magic(), logger=logger) assert handler.is_finished() is False assert handler.is_not_finished() is True handler._finished = True assert handler.is_finished() is True assert handler.is_not_finished() is False @mark.parametrize( 'exception', [StoryscriptError(story=MagicMock(), line={'ln': 1}), Exception()]) @mark.parametrize('story_name', [None, 'super_story']) def test_handle_story_exc(patch, magic, logger, exception, story_name): handler = BaseHandler(magic(), magic(), logger=logger) patch.many(ReportingEvent, ['from_release', 'from_exc']) patch.object(Reporter, 'capture_evt') patch.many(handler, ['set_status', 'finish']) handler.handle_story_exc('app_id', story_name, exception) handler.set_status.assert_called_with(500, 'Story execution failed') handler.finish.assert_called() logger.error.assert_called() if isinstance(exception, StoryscriptError): ReportingEvent.from_release.assert_called_with( exception.story.app.release, Events.APP_REQUEST_ERROR)
def test_asyncy_error(): with raises(StoryscriptError): raise StoryscriptError('things happen')
def test_no_trace_available(): err = StoryscriptError(message='my_message') assert str(err) == 'StoryscriptError: my_message'