Exemple #1
0
    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)
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'
                }

                requests.get(CONFIG["HOST"] + "/emoncms/input/post",
                             params=payload)

                time.sleep(1)
    except Exception:
        ab.capture()
Exemple #3
0
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()