Beispiel #1
0
    def formatException(self, ei):
        etype, exc, tb = ei
        exception_list = traceback2.extract_tb(tb, extract_locals=True)
        if tb:
            caught_list = traceback2.extract_stack(tb.tb_frame)
        else:
            caught_list = traceback2.extract_stack(up=2)
        formatted_exception = traceback2.format_exception_only(etype, exc)
        stack, stackID = GetStack(exception_list,
                                  caught_list,
                                  show_locals=True)
        sio = cStringIO.StringIO()
        for line in stack:
            sio.write(line)

        for line in formatted_exception:
            sio.write(line)

        self._LogThreadLocals(sio)
        self._LogServerInfo(sio)
        try:
            sio.write('Stackhash: {}\n'.format(stackID[0]))
        except Exception:
            pass

        s = sio.getvalue()
        sio.close()
        return s
Beispiel #2
0
 def get_exception_format(self, func, exc):
     try:
         func()
     except exc as value:
         return traceback.format_exception_only(exc, value)
     else:
         raise ValueError("call did not raise exception")
Beispiel #3
0
 def test_format_exception_only_undecodable__str__(self):
     # This won't decode via the ascii codec.
     X = Exception(u('\u5341').encode('shift-jis'))
     err = traceback.format_exception_only(type(X), X)
     self.assertEqual(len(err), 1)
     str_value = "b'\\x8f\\\\'"
     self.assertEqual(err[0], "Exception: %s\n" % str_value)
 def get_exception_format(self, func, exc):
     try:
         func()
     except exc as value:
         return traceback.format_exception_only(exc, value)
     else:
         raise ValueError("call did not raise exception")
 def test_format_exception_only_undecodable__str__(self):
     # This won't decode via the ascii codec.
     X = Exception(u('\u5341').encode('shift-jis'))
     err = traceback.format_exception_only(type(X), X)
     self.assertEqual(len(err), 1)
     str_value = "b'\\x8f\\\\'"
     self.assertEqual(err[0], "Exception: %s\n" % str_value)
Beispiel #6
0
 def test_base_exception(self):
     # Test that exceptions derived from BaseException are formatted right
     e = KeyboardInterrupt()
     lst = traceback.format_exception_only(e.__class__, e)
     self.assertThat(lst,
         MatchesAny(Equals(['KeyboardInterrupt\n']),
                    Equals(['exceptions.KeyboardInterrupt\n'])))
Beispiel #7
0
 def test_format_exception_only_bad__str__(self):
     def qualname(X):
         return getattr(X, '__qualname__', X.__name__)
     class X(Exception):
         def __str__(self):
             1/0
     err = traceback.format_exception_only(X, X())
     self.assertEqual(len(err), 1)
     str_value = '<unprintable %s object>' % X.__name__
     if X.__module__ in ('__main__', 'builtins'):
         str_name = qualname(X)
     else:
         str_name = '.'.join([X.__module__, qualname(X)])
     self.assertEqual(err[0], "%s: %s\n" % (str_name, str_value))
def _LogException(exc_info, extraText, channel, toConsole, toLogServer, toAlertSvc, toMsgWindow, severity, show_locals):
    exctype, exc, tb = exc_info
    exception_list = traceback2.extract_tb(tb, extract_locals=show_locals)
    if tb:
        caught_list = traceback2.extract_stack(tb.tb_frame)
    else:
        caught_list = traceback2.extract_stack(up=2)
    stack, stackID = GetStack(exception_list, caught_list, show_locals=show_locals)
    if severity is None:
        severity = (ERR, WARN)[isinstance(exc, UserError)]
    if toAlertSvc is None:
        toAlertSvc = severity in (ERR,)
    if toMsgWindow and isinstance(exc, UserError) and boot.role == 'client':
        toMsgWindow = 0
        uiMessageFunc(*exc.args)
    multiplexToConsole = False if toLogServer else toConsole
    out = GetMultiplex(channel, severity, multiplexToConsole, toLogServer, toMsgWindow, toAlertSvc, stackID)
    formatted_exception = traceback2.format_exception_only(exctype, exc)
    if not extraText:
        try:
            extraText = 'Info: %s' % formatted_exception[-1].strip()
        except:
            extraText = 'Info: <none>'

    prefix = 'REMOTE ' if channel == 'remote.exc' else ''
    traceID = NextTraceID()
    print >> out, '%sEXCEPTION #%d logged at %s %s : %s ' % (prefix,
     traceID,
     blue.os.FormatUTC()[0],
     blue.os.FormatUTC()[2],
     extraText)
    print >> out, ' '
    print >> out, 'Formatted exception info:',
    for line in formatted_exception:
        print >> out, line,

    print >> out, ' '
    for line in stack:
        print >> out, line,

    print >> out, ' '
    if exctype is MemoryError:
        try:
            DumpMemoryStatus(out)
            DumpMemHistory(out)
        except:
            pass

    try:
        _LogThreadLocals(out)
    except MemoryError:
        pass

    if boot.role != 'client':
        try:
            ram = blue.sysinfo.GetMemory().pageFile / 1024 / 1024
            cpuLoad = sm.GetService('machoNet').GetCPULoad()
            memLeft = blue.sysinfo.GetMemory().availablePhysical / 1024 / 1024
            txt = 'System Information: '
            txt += ' Node ID: %s' % sm.GetService('machoNet').GetNodeID()
            if boot.role == 'server':
                txt += ' | Node Name: %s' % sm.GetService('machoNet').GetLocalHostName()
            txt += ' | Total CPU load: %s%%' % int(cpuLoad)
            txt += ' | Process memory in use: %s MB' % ram
            txt += ' | Physical memory left: %s MB' % memLeft
            print >> out, txt
        except Exception as e:
            sys.exc_clear()

    try:
        print >> out, 'Stackhash: %s' % stackID[0]
    except Exception:
        pass

    print >> out, 'Reported from: ', __name__
    print >> out, '%sEXCEPTION END' % (prefix,)
    out.flush()
    if toConsole:
        if toLogServer:
            print >> sys.stderr, '#nolog: An exception has occurred. It has been logged in the log server as exception #%d' % traceID
        else:
            print >> sys.stderr, 'There is no useful information accompanying this exception in the log server'
Beispiel #9
0
 def test_without_exception(self):
     err = traceback.format_exception_only(None, None)
     self.assertEqual(err, ['None\n'])
Beispiel #10
0
 def test_nocaret(self):
     exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
     err = traceback.format_exception_only(SyntaxError, exc)
     self.assertEqual(len(err), 3)
     self.assertEqual(err[1].strip(), "bad syntax")
 def test_without_exception(self):
     err = traceback.format_exception_only(None, None)
     self.assertEqual(err, ['None\n'])
 def test_nocaret(self):
     exc = SyntaxError("error", ("x.py", 23, None, "bad syntax"))
     err = traceback.format_exception_only(SyntaxError, exc)
     self.assertEqual(len(err), 3)
     self.assertEqual(err[1].strip(), "bad syntax")
Beispiel #13
0
def _LogException(exc_info, extraText, channel, toConsole, toLogServer, toAlertSvc, toMsgWindow, severity, show_locals):
    global traceID
    exctype, exc, tb = exc_info
    exception_list = traceback2.extract_tb(tb, extract_locals=show_locals)
    if tb:
        caught_list = traceback2.extract_stack(tb.tb_frame)
    else:
        caught_list = traceback2.extract_stack(up=2)
    formatted_exception = traceback2.format_exception_only(exctype, exc)
    stack, stackID = GetStack(exception_list, caught_list, show_locals=show_locals)
    if severity is None:
        severity = (ERR, WARN)[isinstance(exc, UserError)]
    if toAlertSvc is None:
        toAlertSvc = severity in (ERR,)
    if toMsgWindow and isinstance(exc, UserError) and boot.role == 'client':
        toMsgWindow = 0
        uiMessageFunc(*exc.args)
    out = GetMultiplex(channel, severity, [toConsole, 0][toLogServer == 1], toLogServer, toMsgWindow, toAlertSvc, stackID)
    pre = ('', 'REMOTE')[channel == 'remote.exc']
    tmpTraceID = traceID
    traceID += 1
    print >> out, '%sEXCEPTION #%d logged at ' % (pre, tmpTraceID), blue.os.FormatUTC()[0], blue.os.FormatUTC()[2], extraText
    for line in stack:
        print >> out, line,

    for line in formatted_exception:
        print >> out, line,

    if exctype is MemoryError:
        try:
            DumpMemoryStatus(out)
            DumpMemHistory(out)
        except:
            pass

    try:
        _LogThreadLocals(out)
    except MemoryError:
        pass

    try:
        print >> out, 'Stackhash:%s\n' % stackID[0]
    except Exception:
        pass

    print >> out
    if boot.role != 'client':
        try:
            nodeID = getattr(sm.services['machoNet'], 'nodeID', None)
            ram = blue.win32.GetProcessMemoryInfo()['PagefileUsage'] / 1024 / 1024
            cpuLoad = sm.GetService('machoNet').GetCPULoad()
            m = blue.win32.GlobalMemoryStatus()
            memLeft = m['AvailPhys'] / 1024 / 1024
            txt = 'System Information: '
            txt += ' Node ID: %s' % sm.GetService('machoNet').GetNodeID()
            if boot.role == 'server':
                txt += ' | Node Name: %s' % sm.GetService('machoNet').GetNodeName()
            txt += ' | Total CPU load: %s%%' % int(cpuLoad)
            txt += ' | Process memory in use: %s MB' % ram
            txt += ' | Physical memory left: %s MB' % memLeft
            print >> out, txt
        except Exception as e:
            sys.exc_clear()

    print >> out, '%sEXCEPTION END' % (pre,)
    out.flush()
    if toConsole:
        if toLogServer:
            print >> sys.stderr, 'An exception has occurred.  It has been logged in the log server as exception #%d' % tmpTraceID
        else:
            print >> sys.stderr, 'There is no useful information accompanying this exception in the log server'
Beispiel #14
0
def _LogException(exc_info, extraText, channel, toConsole, toLogServer,
                  toAlertSvc, toMsgWindow, severity, show_locals):
    exctype, exc, tb = exc_info
    exception_list = traceback2.extract_tb(tb, extract_locals=show_locals)
    if tb:
        caught_list = traceback2.extract_stack(tb.tb_frame)
    else:
        caught_list = traceback2.extract_stack(up=2)
    formatted_exception = traceback2.format_exception_only(exctype, exc)
    stack, stackID = GetStack(exception_list,
                              caught_list,
                              show_locals=show_locals)
    if severity is None:
        severity = (ERR, WARN)[isinstance(exc, UserError)]
    if toAlertSvc is None:
        toAlertSvc = severity in (ERR, )
    if toMsgWindow and isinstance(exc, UserError) and boot.role == 'client':
        toMsgWindow = 0
        uiMessageFunc(*exc.args)
    out = GetMultiplex(channel, severity, [toConsole, 0][toLogServer == 1],
                       toLogServer, toMsgWindow, toAlertSvc, stackID)
    pre = ('', 'REMOTE')[channel == 'remote.exc']
    traceID = NextTraceID()
    print >> out, '%sEXCEPTION #%d logged at ' % (
        pre,
        traceID), blue.os.FormatUTC()[0], blue.os.FormatUTC()[2], extraText
    for line in stack:
        print >> out, line,

    for line in formatted_exception:
        print >> out, line,

    if exctype is MemoryError:
        try:
            DumpMemoryStatus(out)
            DumpMemHistory(out)
        except:
            pass

    try:
        _LogThreadLocals(out)
    except MemoryError:
        pass

    try:
        print >> out, 'Stackhash:%s\n' % stackID[0]
    except Exception:
        pass

    print >> out
    if boot.role != 'client':
        try:
            ram = blue.win32.GetProcessMemoryInfo(
            )['PagefileUsage'] / 1024 / 1024
            cpuLoad = sm.GetService('machoNet').GetCPULoad()
            m = blue.win32.GlobalMemoryStatus()
            memLeft = m['AvailPhys'] / 1024 / 1024
            txt = 'System Information: '
            txt += ' Node ID: %s' % sm.GetService('machoNet').GetNodeID()
            if boot.role == 'server':
                txt += ' | Node Name: %s' % sm.GetService(
                    'machoNet').GetLocalHostName()
            txt += ' | Total CPU load: %s%%' % int(cpuLoad)
            txt += ' | Process memory in use: %s MB' % ram
            txt += ' | Physical memory left: %s MB' % memLeft
            print >> out, txt
        except Exception as e:
            sys.exc_clear()

    print >> out, '%sEXCEPTION END' % (pre, )
    out.flush()
    if toConsole:
        if toLogServer:
            print >> sys.stderr, 'An exception has occurred.  It has been logged in the log server as exception #%d' % traceID
        else:
            print >> sys.stderr, 'There is no useful information accompanying this exception in the log server'
Beispiel #15
0
    def CallUp(self, packet):
        try:
            try:
                return self.ForwardCallUp(packet)
            except (UnMachoDestination,
             MachoException,
             objectCaching.CacheOK,
             util.UpdateMoniker,
             UserError,
             exceptions.CrestSessionExists) as e:
                if isinstance(e, UnMachoDestination) and getattr(packet.destination, 'nodeID', None):
                    if packet.destination.nodeID < const.maxNodeID and macho.mode == 'proxy':
                        self.machoNet.HandleUnMachoDestination(packet.destination.nodeID, packet)
                return packet.ErrorResponse(const.cluster.MACHONETERR_WRAPPEDEXCEPTION, (macho.DumpsSanitized(e),))
            except Exception as e:
                exctype, exc = sys.exc_info()[:2]
                stack, serverStackKey = log.GetStack(traceback2.extract_tb(sys.exc_info()[2], extract_locals=1), show_locals=1)
                dasID = self.GetErrorID()
                desc = ['%s caught an exception while handling a remote call.  The caller should send a traceback with ID %s, which hopefully arrive at the originating node.  Server info follows:' % (macho.mode, dasID)]
                desc += stack
                desc += traceback2.format_exception_only(exctype, exc)
                try:
                    ram = blue.win32.GetProcessMemoryInfo()['PagefileUsage'] / 1024 / 1024
                    cpuLoad = self.machoNet.GetCPULoad()
                    m = blue.win32.GlobalMemoryStatus()
                    memLeft = m['AvailPhys'] / 1024 / 1024
                    txt = 'System Information: '
                    txt += 'Total CPU load: %s%%' % int(cpuLoad)
                    txt += ' | Process memory in use: %s MB' % ram
                    txt += ' | Physical memory left: %s MB\n' % memLeft
                    desc.append(txt)
                    sessionInfo = 'session was ' + str(session)
                    desc.append(sessionInfo)
                except Exception as e:
                    sys.exc_clear()

                with self.machoNet.LogfileError() as f:
                    for each in desc:
                        print >> f, each,

                session.LogSessionHistory('An exception occurred while handling a remote call.  Traceback ID=%s' % dasID)
                session.hasproblems = 1
                sm.GetService('alert').SendStackTraceAlert(serverStackKey, ''.join(desc), 'Delivering Error To Remote Host')
                variables = []
                if macho.mode == 'client':
                    variables += ['Context info logged on the client, session=%s\n' % (strx(session),)]
                elif prefs.clusterMode in ('LOCAL', 'MASTER', 'TRANSLATION') or session and 0 != session.role & (service.ROLE_QA | service.ROLE_PROGRAMMER):
                    variables += ['Context info logged on %s node %d, host=%s\n' % (macho.mode, self.machoNet.GetNodeID(), self.machoNet.GetNodeName())]
                else:
                    variables = []
                    stack = ['//%s/host=%s/nodeID=%d/errorHashKey=%d\n' % (macho.mode,
                      self.machoNet.GetNodeName(),
                      self.machoNet.GetNodeID(),
                      serverStackKey[0])]
                return packet.ErrorResponse(const.cluster.MACHONETERR_WRAPPEDEXCEPTION, (macho.DumpsSanitized(e), (1,
                  stack,
                  variables,
                  dasID,
                  serverStackKey,
                  self.machoNet.GetNodeID(),
                  macho.mode)))

        except:
            log.LogException('Could not create ErrorResponse, sending an empty one')
            return packet.ErrorResponse(const.cluster.MACHONETERR_WRAPPEDEXCEPTION, ())
Beispiel #16
0
def _LogException(exc_info, extraText, channel, toConsole, toLogServer,
                  toAlertSvc, toMsgWindow, severity, show_locals):
    if raven_client:
        raven_client(message=extraText, exc_info=exc_info)
    exctype, exc, tb = exc_info
    exception_list = traceback2.extract_tb(tb, extract_locals=show_locals)
    if tb:
        caught_list = traceback2.extract_stack(tb.tb_frame)
    else:
        caught_list = traceback2.extract_stack(up=2)
    stack, stackID = GetStack(exception_list,
                              caught_list,
                              show_locals=show_locals)
    if severity is None:
        severity = (ERR, WARN)[isinstance(exc, UserError)]
    if toAlertSvc is None:
        toAlertSvc = severity in (ERR, )
    if toMsgWindow and isinstance(exc, UserError) and boot.role == 'client':
        toMsgWindow = 0
        uiMessageFunc(*exc.args)
    multiplexToConsole = False if toLogServer else toConsole
    out = GetMultiplex(channel, severity, multiplexToConsole, toLogServer,
                       toMsgWindow, toAlertSvc, stackID)
    formatted_exception = traceback2.format_exception_only(exctype, exc)
    if not extraText:
        try:
            extraText = 'Info: %s' % formatted_exception[-1].strip()
        except:
            extraText = 'Info: <none>'

    prefix = 'REMOTE ' if channel == 'remote.exc' else ''
    traceID = NextTraceID()
    print >> out, '%sEXCEPTION #%d logged at %s %s : %s ' % (
        prefix, traceID, blue.os.FormatUTC()[0], blue.os.FormatUTC()[2],
        extraText)
    print >> out, ' '
    print >> out, 'Formatted exception info:',
    for line in formatted_exception:
        print >> out, line,

    print >> out, ' '
    for line in stack:
        print >> out, line,

    print >> out, ' '
    if exctype is MemoryError:
        try:
            DumpMemoryStatus(out)
            DumpMemHistory(out)
        except:
            pass

    try:
        _LogThreadLocals(out)
    except MemoryError:
        pass

    if boot.role != 'client':
        try:
            ram = blue.sysinfo.GetMemory().pageFile / 1024 / 1024
            cpuLoad = sm.GetService('machoNet').GetCPULoad()
            memLeft = blue.sysinfo.GetMemory().availablePhysical / 1024 / 1024
            txt = 'System Information: '
            txt += ' Node ID: %s' % sm.GetService('machoNet').GetNodeID()
            if boot.role == 'server':
                txt += ' | Node Name: %s' % sm.GetService(
                    'machoNet').GetLocalHostName()
            txt += ' | Total CPU load: %s%%' % int(cpuLoad)
            txt += ' | Process memory in use: %s MB' % ram
            txt += ' | Physical memory left: %s MB' % memLeft
            print >> out, txt
        except Exception as e:
            sys.exc_clear()

    try:
        print >> out, 'Stackhash: %s' % stackID[0]
    except Exception:
        pass

    print >> out, 'Reported from: ', __name__
    print >> out, '%sEXCEPTION END' % (prefix, )
    out.flush()
    if toConsole:
        if toLogServer:
            print >> sys.stderr, '#nolog: An exception has occurred. It has been logged in the log server as exception #%d' % traceID
        else:
            print >> sys.stderr, 'There is no useful information accompanying this exception in the log server'