def test_client_is_cached_and_can_be_purged(self): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' first_client = get_raven_client() self.assertTrue(first_client) self.assertEquals(id(first_client), id(get_raven_client())) purge_raven_client() self.assertNotEquals(id(first_client), id(get_raven_client()))
def test_client_is_cached_and_can_be_purged(self): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' first_client = get_raven_client() self.assertTrue(first_client) self.assertEquals(id(first_client), id(get_raven_client())) purge_raven_client() self.assertNotEquals(id(first_client), id(get_raven_client()))
def test_404_reporting_can_be_enabled(self): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.request_to_error_view(view='make_404_err') self.assertEquals(0, len(get_raven_client().captureException_calls)) os.environ['RAVEN_ENABLE_EXCEPTIONS'] = 'NotFound' self.request_to_error_view(view='make_404_err') self.assertEquals(1, len(get_raven_client().captureException_calls))
def test_404_reporting_can_be_enabled(self): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.request_to_error_view(view='make_404_err') self.assertEquals(0, len(get_raven_client().captureException_calls)) os.environ['RAVEN_ENABLE_EXCEPTIONS'] = 'NotFound' self.request_to_error_view(view='make_404_err') self.assertEquals(1, len(get_raven_client().captureException_calls))
def test_exceptions_catched_when_client_crashes(self): CrashingClientMock.install() os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.assertEquals(0, get_raven_client().crashes) self.request_to_error_view() self.assertEquals( 2, get_raven_client().crashes, 'We excpect exactly two attempts to report an exception:' ' 1. Report the actual exception,' ' 2. Report the meta exception that the first one failed.')
def test_exceptions_catched_when_client_crashes(self): CrashingClientMock.install() os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.assertEquals(0, get_raven_client().crashes) self.request_to_error_view() self.assertEquals( 2, get_raven_client().crashes, 'We excpect exactly two attempts to report an exception:' ' 1. Report the actual exception,' ' 2. Report the meta exception that the first one failed.')
def test_silent_handling_of_request_preparation_exceptions(self): """When an exception happens while preparing the request, the exception should be catched and reported as raven_meta_error. """ os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.assertEquals(0, len(get_raven_client().captureException_calls)) reporter.maybe_report_exception(None, None, KeyError, None, None) self.assertEquals(1, len(get_raven_client().captureException_calls), 'Expected exactly one error to be reported.') call, = get_raven_client().captureException_calls self.assertIn('raven_meta_error', call['data']['extra'])
def test_silent_handling_of_request_preparation_exceptions(self): """When an exception happens while preparing the request, the exception should be catched and reported as raven_meta_error. """ os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.assertEquals(0, len(get_raven_client().captureException_calls)) reporter.maybe_report_exception(None, None, KeyError, None, None) self.assertEquals(1, len(get_raven_client().captureException_calls), 'Expected exactly one error to be reported.') call, = get_raven_client().captureException_calls self.assertIn('raven_meta_error', call['data']['extra'])
def test_errors_in_requests_are_captured(self): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.request_to_error_view() calls = get_raven_client().captureException_calls self.assertEquals(1, len(calls), 'Expected one raven client call') self.assertEquals(KeyError, calls[0]['exc_info'][0], 'Expected a KeyError to be reported.')
def request_to_error_view(self, view='make_key_error', data=None, login_as=None): # We need to make sure that the raven client mock is created # in this thread and not in the sub-thread created by the # testbrowser, in order to be able to read it later. get_raven_client() with Browser()(self.layer['app']) as browser: if login_as: browser.login(login_as) self.disable_handle_errors(browser) try: browser.open(view=view, data=data) except: pass
def test_errors_in_requests_are_captured(self): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.request_to_error_view(view='raven-test', login_as=SITE_OWNER_NAME) calls = get_raven_client().captureException_calls self.assertEquals(1, len(calls), 'Expected one raven client call') self.assertEquals(RavenConnectionTest, calls[0]['exc_info'][0], 'Expected a RavenConnectionTest to be reported.')
def test_errors_in_requests_are_captured(self): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.request_to_error_view() calls = get_raven_client().captureException_calls self.assertEquals(1, len(calls), 'Expected one raven client call') self.assertEquals(KeyError, calls[0]['exc_info'][0], 'Expected a KeyError to be reported.')
def test_errors_in_requests_are_captured(self): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.request_to_error_view(view='raven-test', login_as=SITE_OWNER_NAME) calls = get_raven_client().captureException_calls self.assertEquals(1, len(calls), 'Expected one raven client call') self.assertEquals(RavenConnectionTest, calls[0]['exc_info'][0], 'Expected a RavenConnectionTest to be reported.')
def request_to_error_view(self, view='make_key_error', data=None, login_as=None): # We need to make sure that the raven client mock is created # in this thread and not in the sub-thread created by the # testbrowser, in order to be able to read it later. get_raven_client() with Browser()(self.layer['app']) as browser: if login_as: browser.login(login_as) # We trigger errors on purpose here, so the testbrowser # should not raise on failed http requests. browser.raise_http_errors = False browser.open(view=view, data=data)
def maybe_report_exception(context, request, exc_type, exc, traceback): if is_exception_type_ignored(exc_type): return exc_info = exc_type, exc, traceback try: client = get_raven_client() if client is None: return try: data = { 'request': prepare_request_infos(request), 'user': prepare_user_infos(context, request), 'extra': prepare_extra_infos(context, request), 'modules': prepare_modules_infos(), 'tags': prepare_tags(exc) } release = get_release() if release: data['release'] = release except: LOG.error('Error while preparing sentry data.') raise try: client.captureException(exc_info=exc_info, data=data) except: LOG.error('Error while reporting to sentry.') raise except: if context: aq_acquire(context, 'error_log').raising(sys.exc_info()) try: get_raven_client().captureException( data={ 'extra': { 'raven_meta_error': 'Error occured while reporting' ' another error.' } }) except: LOG.error('Failed to report error occured while reporting error.')
def maybe_report_exception(context, request, exc_type, exc, traceback): if is_exception_type_ignored(exc_type): return exc_info = exc_type, exc, traceback try: client = get_raven_client() if client is None: return try: data = { "request": prepare_request_infos(request), "user": prepare_user_infos(context, request), "extra": prepare_extra_infos(context, request), "modules": prepare_modules_infos(), "tags": prepare_tags(exc), } release = get_release() if release: data["release"] = release except: LOG.error("Error while preparing sentry data.") raise try: client.captureException(exc_info=exc_info, data=data) except: LOG.error("Error while reporting to sentry.") raise except: if context: aq_acquire(context, "error_log").raising(sys.exc_info()) try: get_raven_client().captureException( data={"extra": {"raven_meta_error": "Error occured while reporting" " another error."}} ) except: LOG.error("Failed to report error occured while reporting error.")
def log_msg_to_sentry(message, context=None, request=None, url=None, data=None, extra=None, fingerprint=None, string_max_length=_marker): """A (hopefully) fail-safe function to log a message to Sentry. This is loosely based on ftw.raven's maybe_report_exception(), except that it can be used to simply log a free-form message and optionally some additional data, for cases where you don't have an actual exception. It also allows to specifiy a fingerprint to better control Sentry's grouping of messages. This still depends on ftw.raven for extraction of some additional info. If either ftw.raven isn't installed, or we can't get hold of a Sentry client, this function should abort gracefully, not log to Sentry, but also not cause any additional problems. This is why everything here is written in a very defensive way, we're being very paranoid and try hard not to cause any additional issues. """ if not FTW_RAVEN_AVAILABLE: log.warn('ftw.raven not installed, not logging to Sentry') return False try: client = get_raven_client() if client is None: log.warn('Could not get raven client, not logging to Sentry') return False if request is None: request = getRequest() if context is None: context = context_from_request(request) if url is None: url = url_from_request(request) try: data_dict = { 'request': prepare_request_infos(request), 'user': prepare_user_infos(context, request), 'extra': prepare_extra_infos(context, request), 'modules': prepare_modules_infos(), 'tags': get_default_tags(), } release = get_release() if release: data_dict['release'] = release if data is not None: data_dict.update(data) except: log.error('Error while preparing sentry data.') raise try: kwargs = dict( message=message, data=data_dict, extra=extra, stack=False, ) if fingerprint: kwargs['fingerprint'] = fingerprint with custom_string_max_length(client, string_max_length): client.captureMessage(**kwargs) except: log.error('Error while reporting to sentry.') raise except: try: get_raven_client().captureException( data={ 'extra': { 'raven_meta_error': 'Error occured while reporting' ' another error.' } }) except: log.error('Failed to report error occured while reporting error.') return False return True
def log_msg_to_sentry(message, context=None, request=None, url=None, data=None, extra=None, fingerprint=None, string_max_length=_marker): """A (hopefully) fail-safe function to log a message to Sentry. This is loosely based on ftw.raven's maybe_report_exception(), except that it can be used to simply log a free-form message and optionally some additional data, for cases where you don't have an actual exception. It also allows to specifiy a fingerprint to better control Sentry's grouping of messages. This still depends on ftw.raven for extraction of some additional info. If either ftw.raven isn't installed, or we can't get hold of a Sentry client, this function should abort gracefully, not log to Sentry, but also not cause any additional problems. This is why everything here is written in a very defensive way, we're being very paranoid and try hard not to cause any additional issues. """ if not FTW_RAVEN_AVAILABLE: log.warn('ftw.raven not installed, not logging to Sentry') return False try: client = get_raven_client() if client is None: log.warn('Could not get raven client, not logging to Sentry') return False if request is None: request = getRequest() if context is None: context = context_from_request(request) if url is None: url = url_from_request(request) try: data_dict = { 'request': prepare_request_infos(request), 'user': prepare_user_infos(context, request), 'extra': prepare_extra_infos(context, request), 'modules': prepare_modules_infos(), 'tags': get_default_tags(), } release = get_release() if release: data_dict['release'] = release if data is not None: data_dict.update(data) except: log.error('Error while preparing sentry data.') raise try: kwargs = dict( message=message, data=data_dict, extra=extra, stack=False, ) if fingerprint: kwargs['fingerprint'] = fingerprint with custom_string_max_length(client, string_max_length): client.captureMessage(**kwargs) except: log.error('Error while reporting to sentry.') raise except: try: get_raven_client().captureException( data={'extra': { 'raven_meta_error': 'Error occured while reporting' ' another error.'}}) except: log.error( 'Failed to report error occured while reporting error.') return False return True
def test_no_client_when_not_configured(self): self.assertIsNone(get_raven_client())
def make_error_and_get_capture_call(self, **kwargs): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.request_to_error_view(**kwargs) calls = get_raven_client().captureException_calls self.assertEquals(1, len(calls), 'Expected one raven client call') return calls[0]
def test_client_is_utility(self): os.environ['RAVEN_DSN'] = dsn = 'https://*****:*****@sentry.local/3' self.assertEquals(dsn, get_raven_client().dsn)
def make_error_and_get_capture_call(self, **kwargs): os.environ['RAVEN_DSN'] = 'https://*****:*****@sentry.local/1' self.request_to_error_view(**kwargs) calls = get_raven_client().captureException_calls self.assertEquals(1, len(calls), 'Expected one raven client call') return calls[0]
def test_client_is_utility(self): os.environ['RAVEN_DSN'] = dsn = 'https://*****:*****@sentry.local/3' self.assertEquals(dsn, get_raven_client().dsn)
def test_no_client_when_not_configured(self): self.assertIsNone(get_raven_client())