def test_notify_error(self): ab = Airbrake(project_id=1234, api_key='fake') try: raise TypeError except Exception as e: exc_info = sys.exc_info() error = Error(exc_info=exc_info) user = {"id": "12345", "name": "root", "email": "*****@*****.**"} notice = ab.build_notice(error, user=user) exception_str = type(e).__name__ exception_type = type(e).__name__ expected_payload = self.get_expected_payload( exception_str, exception_type) data = { 'type': exc_info[1].__class__.__name__, 'backtrace': format_backtrace(exc_info[2]), 'message': pytb_lastline(exc_info) } expected_payload['errors'] = [data] expected_payload['context']['user'] = user expected_payload['context']['severity'] = ErrorLevels.DEFAULT_LEVEL self.assertEqual(expected_payload, notice.payload) with mock.patch('requests.post') as requests_post: ab.notify(error) data = json.loads(requests_post.call_args[1]['data']) self.assertEqual(expected_payload, data)
def test_create_notice_error(self): try: raise TypeError except: exc_info = sys.exc_info() error = Error(exc_info=exc_info, severity=ErrorLevels.WARNING) notice = Notice(error) data = { 'type': exc_info[1].__class__.__name__, 'backtrace': format_backtrace(exc_info[2]), 'message': pytb_lastline(exc_info), 'severity': ErrorLevels.WARNING } expected_payload = {'errors': [data]} self.assertEqual(expected_payload, notice.payload)
def test_create_notice_error(self): try: raise TypeError except TypeError: exc_info = sys.exc_info() error = Error(exc_info=exc_info) notice = Notice(error) data = { 'type': exc_info[1].__class__.__name__, 'backtrace': format_backtrace(exc_info[2]), 'message': pytb_lastline(exc_info) } expected_payload = { 'errors': [data], 'context': {'severity': ErrorLevels.DEFAULT_LEVEL}, } self.assertEqual(expected_payload, notice.payload)
def test_create_notice_error(self): try: raise TypeError except TypeError: exc_info = sys.exc_info() error = Error(exc_info=exc_info) notice = Notice(error) data = { 'type': exc_info[1].__class__.__name__, 'backtrace': format_backtrace(exc_info[2]), 'message': pytb_lastline(exc_info) } expected_payload = { 'errors': [data], 'context': { 'severity': ErrorLevels.DEFAULT_LEVEL }, } self.assertEqual(expected_payload, notice.payload)
def test_capture(self): ab = Airbrake(project_id=1234, api_key='fake') try: raise ValueError("oh nos") except Exception: with mock.patch('requests.post') as requests_post: ab.capture() exc_info = sys.exc_info() raw_frames = traceback.extract_tb(exc_info[2]) exc_frame = raw_frames[0] exception_str = exc_frame[3] exception_type = "ERROR:%s" % exc_frame[0] expected_payload = self.get_expected_payload( exception_str, exception_type) data = { 'type': exc_info[1].__class__.__name__, 'backtrace': format_backtrace(exc_info[2]), 'message': str(exc_frame[3]) } expected_payload['errors'] = [data] expected_payload['context']['severity'] =\ ErrorLevels.DEFAULT_LEVEL err = Error(exc_info=exc_info, filename=str(exc_frame[0]), line=str(exc_frame[1]), function=str(exc_frame[2]), message=str(exc_frame[3]), errtype="ERROR:%s" % str(exc_frame[0])) notice = ab.build_notice(err) self.assertEqual(expected_payload, notice.payload) data = json.loads(requests_post.call_args[1]['data']) self.assertEqual(expected_payload, data)
def test_format(self): tb = self._exc_info()[2] fake_tb = self._make_fake_traceback(tb) self.assertEqual(format_backtrace(tb), format_backtrace(fake_tb))