Пример #1
0
def unexpected_json_error_handler():
    """request.error_response"""

    (typ, value, tb) = _exc_info()
    if typ:
        debug = False
        if hasattr(cherrypy.request, 'params'):
            params = cherrypy.request.params
            debug = 'debug' in params and params['debug']

        response = cherrypy.response
        response.headers['Content-Type'] = 'application/json'
        response.headers.pop('Content-Length', None)
        content = {}
        if isinstance(value, ExtendedHTTPError):
            content.update({'errors': value.errors})
        if isinstance(typ, cherrypy.HTTPError):
            cherrypy._cperror.clean_headers(value.code)
            response.status = value.status
            content.update({'code': value.code, 'reason': value.reason,
                            'message': value._message})
        elif isinstance(typ, (TypeError, ValueError, KeyError)):
            cherrypy._cperror.clean_headers(400)
            response.status = 400
            reason, default_message = _httputil.response_codes[400]
            content = {'code': 400, 'reason': reason,
                       'message': value.message or default_message}

        if cherrypy.serving.request.show_tracebacks or debug:
            tb = _format_exc()
            content['traceback'] = tb
        response.body = json.dumps(content).encode('utf-8')
Пример #2
0
    def _bootstrap_inner(self):
        try:
            self._set_ident()
            self._set_tstate_lock()
            self._started.set()
            with _active_limbo_lock:
                _active[self._ident] = self
                del _limbo[self]
            if _trace_hook:
                _sys.settrace(_trace_hook)
            if _profile_hook:
                _sys.setprofile(_profile_hook)
            try:
                try:
                    self.run()
                except SystemExit:
                    pass
                except:
                    if _sys:
                        pass
                    if _sys.stderr is not None:
                        print(('Exception in thread %s:\n%s' %
                               (self.name, _format_exc())),
                              file=(_sys.stderr))
                    elif self._stderr is not None:
                        exc_type, exc_value, exc_tb = self._exc_info()
                        try:
                            print((
                                'Exception in thread ' + self.name +
                                ' (most likely raised during interpreter shutdown):'
                            ),
                                  file=(self._stderr))
                            print('Traceback (most recent call last):',
                                  file=(self._stderr))
                            while exc_tb:
                                print(('  File "%s", line %s, in %s' %
                                       (exc_tb.tb_frame.f_code.co_filename,
                                        exc_tb.tb_lineno,
                                        exc_tb.tb_frame.f_code.co_name)),
                                      file=(self._stderr))
                                exc_tb = exc_tb.tb_next

                            print(('%s: %s' % (exc_type, exc_value)),
                                  file=(self._stderr))
                            self._stderr.flush()
                        finally:
                            del exc_type
                            del exc_value
                            del exc_tb

            finally:
                pass

        finally:
            with _active_limbo_lock:
                try:
                    del _active[get_ident()]
                except:
                    pass
    def __bootstrap_inner(self):
        try:
            self._set_ident()
            self.__started.set()
            with _active_limbo_lock:
                _active[self.__ident] = self
                del _limbo[self]
            self._note('%s.__bootstrap(): thread started', self)
            if _trace_hook:
                self._note('%s.__bootstrap(): registering trace hook', self)
                _sys.settrace(_trace_hook)
            if _profile_hook:
                self._note('%s.__bootstrap(): registering profile hook', self)
                _sys.setprofile(_profile_hook)
            try:
                self.run()
            except SystemExit:
                self._note('%s.__bootstrap(): raised SystemExit', self)
            except:
                self._note('%s.__bootstrap(): unhandled exception', self)
                if _sys and _sys.stderr is not None:
                    print >> _sys.stderr, 'Exception in thread %s:\n%s' % (
                        self.name, _format_exc())
                elif self.__stderr is not None:
                    exc_type, exc_value, exc_tb = self.__exc_info()
                    try:
                        print >> self.__stderr, 'Exception in thread ' + self.name + ' (most likely raised during interpreter shutdown):'
                        print >> self.__stderr, 'Traceback (most recent call last):'
                        while exc_tb:
                            print >> self.__stderr, '  File "%s", line %s, in %s' % (
                                exc_tb.tb_frame.f_code.co_filename,
                                exc_tb.tb_lineno,
                                exc_tb.tb_frame.f_code.co_name)
                            exc_tb = exc_tb.tb_next

                        print >> self.__stderr, '%s: %s' % (exc_type,
                                                            exc_value)
                    finally:
                        del exc_type
                        del exc_value
                        del exc_tb

            else:
                self._note('%s.__bootstrap(): normal return', self)
            finally:
                self.__exc_clear()

        finally:
            with _active_limbo_lock:
                self.__stop()
                try:
                    del _active[_get_ident()]
                except:
                    pass

        return
Пример #4
0
def format_exc(*args, **kw):
    L = []

    for line in _format_exc(*args, **kw).split('\n'):
        line = line.replace('langlynx', 'langlynx')
        line = line.replace('david', 'langlynx')

        if line.strip().startswith('File "/home/ll/Dev/git/'):
            line = line.replace('/home/ll/Dev/git/', '')
        L.append(line)

    return '\n'.join(L)
Пример #5
0
    def __bootstrap_inner(self):
        try:
            self._set_ident()
            self.__started.set()
            with _active_limbo_lock:
                _active[self.__ident] = self
                del _limbo[self]
            self._note("%s.__bootstrap(): thread started", self)
            if _trace_hook:
                self._note("%s.__bootstrap(): registering trace hook", self)
                _sys.settrace(_trace_hook)
            if _profile_hook:
                self._note("%s.__bootstrap(): registering profile hook", self)
                _sys.setprofile(_profile_hook)
            try:
                self.run()
            except SystemExit:
                self._note("%s.__bootstrap(): raised SystemExit", self)
            except:
                self._note("%s.__bootstrap(): unhandled exception", self)
                if _sys:
                    _sys.stderr.write("Exception in thread %s:\n%s\n" % (self.name, _format_exc()))
                else:
                    exc_type, exc_value, exc_tb = self.__exc_info()
                    try:
                        print >> self.__stderr, "Exception in thread " + self.name + " (most likely raised during interpreter shutdown):"
                        print >> self.__stderr, "Traceback (most recent call last):"
                        while exc_tb:
                            print >> self.__stderr, '  File "%s", line %s, in %s' % (
                                exc_tb.tb_frame.f_code.co_filename,
                                exc_tb.tb_lineno,
                                exc_tb.tb_frame.f_code.co_name,
                            )
                            exc_tb = exc_tb.tb_next

                        print >> self.__stderr, "%s: %s" % (exc_type, exc_value)
                    finally:
                        del exc_type
                        del exc_value
                        del exc_tb

            else:
                self._note("%s.__bootstrap(): normal return", self)
            finally:
                self.__exc_clear()

        finally:
            with _active_limbo_lock:
                self.__stop()
                try:
                    del _active[_get_ident()]
                except:
                    pass
Пример #6
0
def handler(exc=None, passthru=True):
    if exc is None:
        exc = sys.exc_info()
    TEXT = _format_exc(exc)
    try:
        _thread = _currentThread()
        TEXT = ("Exception in thread %s:\n" % _thread.name) + TEXT
    except: pass
    CMD = len(sys.argv) > 1 and sys.argv[1].endswith('.py') and sys.argv[1] or sys.argv[0]
    SUBJECT = CMD+': '+str(exc[1]).replace('\r','').replace('\n','')
    HEADERS = 'From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n' % (EMAIL_FROM, EMAIL_TO, SUBJECT)
    _SMTP('localhost').sendmail(EMAIL_FROM, [EMAIL_TO], HEADERS+TEXT)
    if passthru:
        sys.__excepthook__(*exc)
    return TEXT
    def __bootstrap(self):
        try:
            self.__started = True
            _active_limbo_lock.acquire()
            _active[_get_ident()] = self
            del _limbo[self]
            _active_limbo_lock.release()
            if _trace_hook:
                self._note('%s.__bootstrap(): registering trace hook', self)
                _sys.settrace(_trace_hook)
            if _profile_hook:
                self._note('%s.__bootstrap(): registering profile hook', self)
                _sys.setprofile(_profile_hook)
            try:
                self.run()
            except SystemExit:
                pass
            except:
                if _sys:
                    _sys.stderr.write('Exception in thread %s:\n%s\n' %
                                      (self.getName(), _format_exc()))
                else:
                    exc_type, exc_value, exc_tb = self.__exc_info()
                    try:
                        print >> self.__stderr, 'Exception in thread ' + self.getName(
                        ) + ' (most likely raised during interpreter shutdown):'
                        print >> self.__stderr, 'Traceback (most recent call last):'
                        while exc_tb:
                            print >> self.__stderr, '  File "%s", line %s, in %s' % (
                                exc_tb.tb_frame.f_code.co_filename,
                                exc_tb.tb_lineno,
                                exc_tb.tb_frame.f_code.co_name)
                            exc_tb = exc_tb.tb_next

                        print >> self.__stderr, '%s: %s' % (exc_type,
                                                            exc_value)
                    finally:
                        del exc_type
                        del exc_value
                        del exc_tb

        finally:
            self.__stop()
            try:
                self.__delete()
            except:
                pass
Пример #8
0
 def _bootstrap_inner(self):
     try:
         self._set_ident()
         self._started.set()
         with _active_limbo_lock:
             _active[self._ident] = self
             del _limbo[self]
         if _trace_hook:
             _sys.settrace(_trace_hook)
         if _profile_hook:
             _sys.setprofile(_profile_hook)
         try:
             self.run()
         except SystemExit:
             pass
         except:
             if _sys:
                 _sys.stderr.write('Exception in thread %s:\n%s\n' % (self.name, _format_exc()))
             else:
                 (exc_type, exc_value, exc_tb) = self._exc_info()
                 try:
                     print('Exception in thread ' + self.name + ' (most likely raised during interpreter shutdown):', file=self._stderr)
                     print('Traceback (most recent call last):', file=self._stderr)
                     while exc_tb:
                         print('  File "%s", line %s, in %s' % (exc_tb.tb_frame.f_code.co_filename, exc_tb.tb_lineno, exc_tb.tb_frame.f_code.co_name), file=self._stderr)
                         exc_tb = exc_tb.tb_next
                     print('%s: %s' % (exc_type, exc_value), file=self._stderr)
                 finally:
                     del exc_type
                     del exc_value
                     del exc_tb
         finally:
             pass
     finally:
         with _active_limbo_lock:
             self._stop()
             try:
                 del _active[get_ident()]
             except:
                 pass
Пример #9
0
def unexpected_json_error_handler():
    """request.error_response"""

    (typ, value, tb) = _exc_info()
    if typ:
        debug = False
        if hasattr(cherrypy.request, 'params'):
            params = cherrypy.request.params
            debug = 'debug' in params and params['debug']

        response = cherrypy.response
        response.headers['Content-Type'] = 'application/json'
        response.headers.pop('Content-Length', None)
        content = {}
        if isinstance(value, ExtendedHTTPError):
            content.update({'errors': value.errors})
        if isinstance(typ, cherrypy.HTTPError):
            cherrypy._cperror.clean_headers(value.code)
            response.status = value.status
            content.update({
                'code': value.code,
                'reason': value.reason,
                'message': value._message
            })
        elif isinstance(typ, (TypeError, ValueError, KeyError)):
            cherrypy._cperror.clean_headers(400)
            response.status = 400
            reason, default_message = _httputil.response_codes[400]
            content = {
                'code': 400,
                'reason': reason,
                'message': value.message or default_message
            }

        if cherrypy.serving.request.show_tracebacks or debug:
            tb = _format_exc()
            content['traceback'] = tb
        response.body = json.dumps(content).encode('utf-8')
Пример #10
0
    def _bootstrap_inner(self):
        try:
            self._set_ident()
            self._set_tstate_lock()
            self._started.set()
            with _active_limbo_lock:
                _active[self._ident] = self
                del _limbo[self]

            if _trace_hook:
                _sys.settrace(_trace_hook)
            if _profile_hook:
                _sys.setprofile(_profile_hook)

            try:
                self.run()
            except SystemExit:
                pass
            except:
                # If sys.stderr is no more (most likely from interpreter
                # shutdown) use self._stderr.  Otherwise still use sys (as in
                # _sys) in case sys.stderr was redefined since the creation of
                # self.
                if _sys:
                    _sys.stderr.write("Exception in thread %s:\n%s\n" %
                                      (self.name, _format_exc()))
                else:
                    # Do the best job possible w/o a huge amt. of code to
                    # approximate a traceback (code ideas from
                    # Lib/traceback.py)
                    exc_type, exc_value, exc_tb = self._exc_info()
                    try:
                        print((
                            "Exception in thread " + self.name +
                            " (most likely raised during interpreter shutdown):"
                        ),
                              file=self._stderr)
                        print(("Traceback (most recent call last):"),
                              file=self._stderr)
                        while exc_tb:
                            print(('  File "%s", line %s, in %s' %
                                   (exc_tb.tb_frame.f_code.co_filename,
                                    exc_tb.tb_lineno,
                                    exc_tb.tb_frame.f_code.co_name)),
                                  file=self._stderr)
                            exc_tb = exc_tb.tb_next
                        print(("%s: %s" % (exc_type, exc_value)),
                              file=self._stderr)
                    # Make sure that exc_tb gets deleted since it is a memory
                    # hog; deleting everything else is just for thoroughness
                    finally:
                        del exc_type, exc_value, exc_tb
            finally:
                # Prevent a race in
                # test_threading.test_no_refcycle_through_target when
                # the exception keeps the target alive past when we
                # assert that it's dead.
                #XXX self.__exc_clear()
                pass
        finally:
            with _active_limbo_lock:
                try:
                    # We don't call self._delete() because it also
                    # grabs _active_limbo_lock.
                    del _active[get_ident()]
                except:
                    pass
Пример #11
0
    def __bootstrap_inner(self):
        try:
            self._set_ident()
            self.__started.set()
            with _active_limbo_lock:
                _active[self.__ident] = self
                del _limbo[self]
            if __debug__:
                self._note("%s.__bootstrap(): thread started", self)

            if _trace_hook:
                self._note("%s.__bootstrap(): registering trace hook", self)
                _sys.settrace(_trace_hook)
            if _profile_hook:
                self._note("%s.__bootstrap(): registering profile hook", self)
                _sys.setprofile(_profile_hook)

            try:
                self.run()
            except SystemExit:
                if __debug__:
                    self._note("%s.__bootstrap(): raised SystemExit", self)
            except:
                if __debug__:
                    self._note("%s.__bootstrap(): unhandled exception", self)
                # If sys.stderr is no more (most likely from interpreter
                # shutdown) use self.__stderr.  Otherwise still use sys (as in
                # _sys) in case sys.stderr was redefined since the creation of
                # self.
                if _sys:
                    _sys.stderr.write("Exception in thread %s:\n%s\n" % (self.name, _format_exc()))
                else:
                    # Do the best job possible w/o a huge amt. of code to
                    # approximate a traceback (code ideas from
                    # Lib/traceback.py)
                    exc_type, exc_value, exc_tb = self.__exc_info()
                    try:
                        print >> self.__stderr, (
                            "Exception in thread " + self.name + " (most likely raised during interpreter shutdown):"
                        )
                        print >> self.__stderr, ("Traceback (most recent call last):")
                        while exc_tb:
                            print >> self.__stderr, (
                                '  File "%s", line %s, in %s'
                                % (exc_tb.tb_frame.f_code.co_filename, exc_tb.tb_lineno, exc_tb.tb_frame.f_code.co_name)
                            )
                            exc_tb = exc_tb.tb_next
                        print >> self.__stderr, ("%s: %s" % (exc_type, exc_value))
                    # Make sure that exc_tb gets deleted since it is a memory
                    # hog; deleting everything else is just for thoroughness
                    finally:
                        del exc_type, exc_value, exc_tb
            else:
                if __debug__:
                    self._note("%s.__bootstrap(): normal return", self)
            finally:
                # Prevent a race in
                # test_threading.test_no_refcycle_through_target when
                # the exception keeps the target alive past when we
                # assert that it's dead.
                self.__exc_clear()
        finally:
            with _active_limbo_lock:
                self.__stop()
                try:
                    # We don't call self.__delete() because it also
                    # grabs _active_limbo_lock.
                    del _active[_get_ident()]
                except:
                    pass
Пример #12
0
    def __bootstrap(self):
        try:
            self.__started = True
            _active_limbo_lock.acquire()
            _active[_get_ident()] = self
            del _limbo[self]
            _active_limbo_lock.release()
            if __debug__:
                self._note("%s.__bootstrap(): thread started", self)

            if _trace_hook:
                self._note("%s.__bootstrap(): registering trace hook", self)
                _sys.settrace(_trace_hook)
            if _profile_hook:
                self._note("%s.__bootstrap(): registering profile hook", self)
                _sys.setprofile(_profile_hook)

            try:
                self.run()
            except SystemExit:
                if __debug__:
                    self._note("%s.__bootstrap(): raised SystemExit", self)
            except:
                if __debug__:
                    self._note("%s.__bootstrap(): unhandled exception", self)
                # If sys.stderr is no more (most likely from interpreter
                # shutdown) use self.__stderr.  Otherwise still use sys (as in
                # _sys) in case sys.stderr was redefined since the creation of
                # self.
                if _sys:
                    _sys.stderr.write("Exception in thread %s:\n%s\n" %
                                      (self.getName(), _format_exc()))
                else:
                    # Do the best job possible w/o a huge amt. of code to
                    # approximate a traceback (code ideas from
                    # Lib/traceback.py)
                    exc_type, exc_value, exc_tb = self.__exc_info()
                    try:
                        print >> self.__stderr, (
                            "Exception in thread " + self.getName() +
                            " (most likely raised during interpreter shutdown):"
                        )
                        print >> self.__stderr, (
                            "Traceback (most recent call last):")
                        while exc_tb:
                            print >> self.__stderr, (
                                '  File "%s", line %s, in %s' %
                                (exc_tb.tb_frame.f_code.co_filename,
                                 exc_tb.tb_lineno,
                                 exc_tb.tb_frame.f_code.co_name))
                            exc_tb = exc_tb.tb_next
                        print >> self.__stderr, ("%s: %s" %
                                                 (exc_type, exc_value))
                    # Make sure that exc_tb gets deleted since it is a memory
                    # hog; deleting everything else is just for thoroughness
                    finally:
                        del exc_type, exc_value, exc_tb
            else:
                if __debug__:
                    self._note("%s.__bootstrap(): normal return", self)
        finally:
            self.__stop()
            try:
                self.__delete()
            except:
                pass
Пример #13
0
    def __bootstrap(self):
        try:
            self.__started = True
            _active_limbo_lock.acquire()
            _active[_get_ident()] = self
            del _limbo[self]
            _active_limbo_lock.release()
            if __debug__:
                self._note("%s.__bootstrap(): thread started", self)

            if _trace_hook:
                self._note("%s.__bootstrap(): registering trace hook", self)
                _sys.settrace(_trace_hook)
            if _profile_hook:
                self._note("%s.__bootstrap(): registering profile hook", self)
                _sys.setprofile(_profile_hook)

            try:
                self.run()
            except SystemExit:
                if __debug__:
                    self._note("%s.__bootstrap(): raised SystemExit", self)
            except:
                if __debug__:
                    self._note("%s.__bootstrap(): unhandled exception", self)
                # If sys.stderr is no more (most likely from interpreter
                # shutdown) use self.__stderr.  Otherwise still use sys (as in
                # _sys) in case sys.stderr was redefined since the creation of
                # self.
                if _sys:
                    _sys.stderr.write("Exception in thread %s:\n%s\n" %
                                      (self.getName(), _format_exc()))
                else:
                    # Do the best job possible w/o a huge amt. of code to
                    # approximate a traceback (code ideas from
                    # Lib/traceback.py)
                    exc_type, exc_value, exc_tb = self.__exc_info()
                    try:
                        print>>self.__stderr, (
                            "Exception in thread " + self.getName() +
                            " (most likely raised during interpreter shutdown):")
                        print>>self.__stderr, (
                            "Traceback (most recent call last):")
                        while exc_tb:
                            print>>self.__stderr, (
                                '  File "%s", line %s, in %s' %
                                (exc_tb.tb_frame.f_code.co_filename,
                                    exc_tb.tb_lineno,
                                    exc_tb.tb_frame.f_code.co_name))
                            exc_tb = exc_tb.tb_next
                        print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
                    # Make sure that exc_tb gets deleted since it is a memory
                    # hog; deleting everything else is just for thoroughness
                    finally:
                        del exc_type, exc_value, exc_tb
            else:
                if __debug__:
                    self._note("%s.__bootstrap(): normal return", self)
        finally:
            self.__stop()
            try:
                self.__delete()
            except:
                pass
Пример #14
0
    def __bootstrap(self):
        try:
            self.__started = True
            _active_limbo_lock.acquire()
            _active[_get_ident()] = self
            del _limbo[self]
            _active_limbo_lock.release()
            if _trace_hook:
                self._note('%s.__bootstrap(): registering trace hook', self)
                _sys.settrace(_trace_hook)
            if _profile_hook:
                self._note('%s.__bootstrap(): registering profile hook', self)
                _sys.setprofile(_profile_hook)
            try:
                self.run()
            except SystemExit:
                pass
            except:
                if _sys:
                    _sys.stderr.write('Exception in thread %s:\n%s\n' % (self.getName(), _format_exc()))
                else:
                    exc_type, exc_value, exc_tb = self.__exc_info()
                    try:
                        print >> self.__stderr, 'Exception in thread ' + self.getName() + ' (most likely raised during interpreter shutdown):'
                        print >> self.__stderr, 'Traceback (most recent call last):'
                        while exc_tb:
                            print >> self.__stderr, '  File "%s", line %s, in %s' % (exc_tb.tb_frame.f_code.co_filename, exc_tb.tb_lineno, exc_tb.tb_frame.f_code.co_name)
                            exc_tb = exc_tb.tb_next

                        print >> self.__stderr, '%s: %s' % (exc_type, exc_value)
                    finally:
                        del exc_type
                        del exc_value
                        del exc_tb

        finally:
            self.__stop()
            try:
                self.__delete()
            except:
                pass