Example #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': 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)
Example #2
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)
Example #3
0
def build_error(exc_info=None, message=None, filename=None,
                line=None, function=None, errtype=None, **params):
    """Build an error instance with the current exception."""
    if not utils.is_exc_info_tuple(exc_info) or not exc_info:
        exc_info = sys.exc_info()
        raw_frames = traceback.extract_tb(exc_info[2])
        exc_frame = raw_frames[0]
    return Error(exc_info=exc_info,
                 filename=filename or str(exc_frame[0]),
                 line=line or str(exc_frame[1]),
                 function=function or str(exc_frame[2]),
                 message=message or str(exc_frame[3]),
                 errtype=errtype or "ERROR:%s" % str(exc_frame[0]),
                 **params)
Example #4
0
def build_error(exc_info=None, message=None, filename=None,
                line=None, function=None, errtype=None, **params):
    """Build an error instance with the current exception."""
    if not utils.is_exc_info_tuple(exc_info) or not exc_info:
        exc_info = sys.exc_info()
        raw_frames = traceback.extract_tb(exc_info[2])
        exc_frame = raw_frames[0]
    return Error(exc_info=exc_info,
                 filename=filename or str(exc_frame[0]),
                 line=line or str(exc_frame[1]),
                 function=function or str(exc_frame[2]),
                 message=message or str(exc_frame[3]),
                 errtype=errtype or "ERROR:%s" % str(exc_frame[0]),
                 **params)
Example #5
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)
Example #6
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))
Example #7
0
 def test_is_exc_info_tuple(self):
     exc_info = self._exc_info()
     self.assertTrue(is_exc_info_tuple(exc_info))
     fake_tb = self._make_fake_traceback(exc_info[2])
     self.assertTrue(is_exc_info_tuple((exc_info[0], exc_info[1], fake_tb)))