Exemple #1
0
    def __init__(self, exc_info=None, message=None, filename=None,
                 line=None, function=None, errtype=None):

        self.exc_info = exc_info

        # default datastructure
        self.data = {
            'type': errtype or "Record",
            'backtrace': [{'file': filename or "N/A",
                           'line': line or 1,
                           'function': function or "N/A"}],
            'message': message or "N/A"}

        if utils.is_exc_info_tuple(self.exc_info):
            if not all(self.exc_info):
                return
            tbmessage = utils.pytb_lastline(self.exc_info)
            self.data.update(
                {'type': self.exc_info[1].__class__.__name__,
                 'backtrace': format_backtrace(self.exc_info[2]),
                 'message': tbmessage})
        else:
            raise TypeError(
                "Airbrake module (notifier.Error) received "
                "unsupported 'exc_info' type. Should be a "
                "3-piece tuple as returned by sys.exc_info(). "
                "Invalid argument was %s"
                % self.exc_info)
Exemple #2
0
    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)
Exemple #3
0
    def __init__(self, exc_info=None, message=None, filename=None,
                 line=None, function=None, errtype=None):

        self.exc_info = exc_info

        # default datastructure
        self.data = {
            'type': errtype or "Record",
            'backtrace': [{'file': filename or "N/A",
                           'line': line or 1,
                           'function': function or "N/A"}],
            'message': message or "N/A"}

        if utils.is_exc_info_tuple(self.exc_info):
            if not all(self.exc_info):
                return
            tbmessage = utils.pytb_lastline(self.exc_info)
            self.data.update(
                {'type': self.exc_info[1].__class__.__name__,
                 'backtrace': format_backtrace(self.exc_info[2]),
                 'message': message or tbmessage})
        else:
            raise TypeError(
                "Airbrake module (notifier.Error) received "
                "unsupported 'exc_info' type. Should be a "
                "3-piece tuple as returned by sys.exc_info(). "
                "Invalid argument was %s"
                % self.exc_info)
Exemple #4
0
    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)
Exemple #7
0
    def log(self, exc_info=None, message=None, filename=None,
            line=None, function=None, errtype=None, **params):
        """Acknowledge an error and post it to airbrake.io.

        :param exc_info:    Exception tuple to use for formatting request.
                            If None, sys.exc_info() will be read to get
                            exception info. To prevent the reading of
                            sys.exc_info(), set exc_info to False.
        :param message:     Message accompanying error.
        :param filename:    Name of file where error occurred.
        :param line:        Line number in file where error occurred.
        :param function:    Function name where error occurred.
        :param errtype:     Type of error which occurred.
        :param params:      Payload field "params" which may contain any other
                            context related to the exception(s).

        Exception info willl be read from sys.exc_info() if it is not
        supplied. To prevent this behavior, pass exc_info=False.
        """

        if not utils.is_exc_info_tuple(exc_info):
            # compatibility, allows exc_info not to be a exc tuple
            errmessage = None
            if isinstance(exc_info, Exception):
                errmessage = utils.pytb_lastline(exc_info)
            elif exc_info:
                try:
                    errmessage = json.dumps(exc_info)
                except (ValueError, TypeError):
                    errmessage = str(exc_info)
            if errmessage:
                # this way, if exc_info kwarg is passed to logger method,
                # its value will be available in params
                params['exc_info'] = errmessage

            if message and errmessage and message != errmessage:
                message = "%s | %s" % (message, errmessage)
            elif errmessage:
                message = errmessage

            # read the global exception stack, but
            # be sure not to read the same exception twice.
            # This can happen for a number of reasons:
            #   - the exception context has not been
            #     cleared or garbage collected
            #   - the airbrake logging handler is being used
            #     to log exceptions and error messages in the same
            #     thread, but that thread is not actually logging
            #     some exceptions, or is not clearing the exception
            #     context via sys.exc_clear()
            # the alternative is to clear the exception stack every time,
            # which this module *should not* take the liberty to do
            #
            # to prevent this function from reading the global
            # exception context *at all*, pass exc_info=False
            if exc_info is False:
                exc_info = None, None, None
            else:
                exc_info = sys.exc_info()

        # dont ship the same exception instance twice
        if exc_info[1] in self._exc_queue:
            exc_info = None, None, None
        self._exc_queue.put(exc_info[1])

        error = Error(
            exc_info=exc_info, message=message, filename=filename,
            line=line, function=function, errtype=errtype)
        environment = params.pop('environment', {})
        session = params.pop('session', {})
        payload = {'context': self.context,
                   'params': params,
                   'errors': [error.data],
                   'notifier': self.notifier,
                   'environment': environment,
                   'session': session}

        return self.notify(payload)
Exemple #8
0
    def log(self,
            exc_info=None,
            message=None,
            filename=None,
            line=None,
            function=None,
            errtype=None,
            **params):
        """Acknowledge an error and post it to airbrake.io.

        :param exc_info:    Exception tuple to use for formatting request.
                            If None, sys.exc_info() will be read to get
                            exception info. To prevent the reading of
                            sys.exc_info(), set exc_info to False.
        :param message:     Message accompanying error.
        :param filename:    Name of file where error occurred.
        :param line:        Line number in file where error occurred.
        :param function:    Function name where error occurred.
        :param errtype:     Type of error which occurred.
        :param params:      Payload field "params" which may contain any other
                            context related to the exception(s).

        Exception info willl be read from sys.exc_info() if it is not
        supplied. To prevent this behavior, pass exc_info=False.
        """
        if not utils.is_exc_info_tuple(exc_info):
            # compatibility, allows exc_info not to be a exc tuple
            errmessage = None
            if isinstance(exc_info, Exception):
                errmessage = utils.pytb_lastline(exc_info)
            elif exc_info:
                try:
                    errmessage = json.dumps(exc_info)
                except (ValueError, TypeError):
                    errmessage = str(exc_info)
            if errmessage:
                # this way, if exc_info kwarg is passed to logger method,
                # its value will be available in params
                params['exc_info'] = errmessage

            if message and errmessage and message != errmessage:
                message = "%s | %s" % (message, errmessage)
            elif errmessage:
                message = errmessage

            # read the global exception stack, but
            # be sure not to read the same exception twice.
            # This can happen for a number of reasons:
            #   - the exception context has not been
            #     cleared or garbage collected
            #   - the airbrake logging handler is being used
            #     to log exceptions and error messages in the same
            #     thread, but that thread is not actually logging
            #     some exceptions, or is not clearing the exception
            #     context via sys.exc_clear()
            # the alternative is to clear the exception stack every time,
            # which this module *should not* take the liberty to do
            #
            # to prevent this function from reading the global
            # exception context *at all*, pass exc_info=False
            if exc_info is False:
                exc_info = None, None, None
            else:
                exc_info = sys.exc_info()

        # dont ship the same exception instance twice
        if exc_info[1] in self._exc_queue:
            exc_info = None, None, None
        self._exc_queue.put(exc_info[1])

        error = Error(exc_info=exc_info,
                      message=message,
                      filename=filename,
                      line=line,
                      function=function,
                      errtype=errtype)
        environment = params.pop('environment', {})
        session = params.pop('session', {})
        payload = {
            'context': self.context,
            'params': params,
            'errors': [error.data],
            'notifier': self.notifier,
            'environment': environment,
            'session': session
        }
        return self.notify(json.dumps(payload, cls=utils.FailProofJSONEncoder))