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_notice_severity(self): ab = Airbrake(project_id=1234, api_key='fake') notice = ab.build_notice(ValueError("This is a test"), severity=ErrorLevels.CRITICAL) self.assertEqual(ErrorLevels.CRITICAL, notice.payload['context']['severity']) with mock.patch('requests.post') as requests_post: ab.notify(notice) data = json.loads(requests_post.call_args[1]['data']) error_level = data['context']['severity'] self.assertEqual(ErrorLevels.CRITICAL, error_level)
def test_notify_str(self): ab = Airbrake(project_id=1234, api_key='fake') exception_str = "This is a test" exception_type = 'Error' notice = ab.build_notice(exception_str) expected_payload = self.get_expected_payload(exception_str, exception_type) self.assertEqual(expected_payload, notice.payload) with mock.patch('requests.post') as requests_post: ab.notify(exception_str) data = json.loads(requests_post.call_args[1]['data']) self.assertEqual(expected_payload, data)
def check_timeout(self, timeout=None, expected_timeout=None): ab = Airbrake(project_id=1234, api_key='fake', environment='test', timeout=timeout) if not timeout: ab = Airbrake(project_id=1234, api_key='fake', environment='test') with mock.patch('requests.post') as requests_post: ab.deploy('test', 'user1') timeout = requests_post.call_args[1]['timeout'] self.assertEqual(expected_timeout, timeout) with mock.patch('requests.post') as requests_post: notice = ab.build_notice("This is a test") ab.notify(notice) timeout = requests_post.call_args[1]['timeout'] self.assertEqual(expected_timeout, timeout)
def readAll(self): return self.readCurrent(), self.readPower() def close(self): self.ser.close() if __name__ == "__main__": try: sensor = BTPOWER() if sensor.isReady(): # init message print("Reading and sending to emoncms...") ab.notify("Reading and sending to emoncms...") while True: current, power = sensor.readAll() json_inputs = json.dumps({ 'current': str(current), 'power': str(power) }) payload = { 'apikey': CONFIG["API_KEY"], 'fulljson': json_inputs, 'node': 'emontx' }
from airbrake.notifier import Airbrake manager = Airbrake(project_id=144031, api_key="5a2fb879e83b40479b90284263193376") try: # Raising an exception. 1 / 0 except ZeroDivisionError as e: # Sends a 'division by zero' exception to Airbrake. manager.notify(e) except: # Sends all other exceptions to Airbrake. manager.capture()