def onerror(self, e):
     """Send here Exception and traceback by Error channel
     """
     f = Formatter()
     history = json.dumps(self.channel_history,
                          indent=2,
                          default=datahandler)
     data = (history, xml_escape(f.formatException(sys.exc_info())),)
     if EXCEPTION_FLAVOR == 'html':
         traceback = '<pre>%s\n\n%s</pre>' % data
     else:
         traceback = '%s\n%s' % data
     if DEBUG:
         err_message = {'channel': WS_CHANNELS['ERROR_CHID'],
                        'pkg': {'exception': repr(e), 'tb': traceback}}
     else:
         err_message = {'channel': WS_CHANNELS['ERROR_CHID'],
                        'pkg': {'exception': 'error 500',
                                'tb': 'an error occurred'}}
     log.error(format_exception(e, None), extra=self.channel_history)
     report_exception(e, extra=self.channel_history)
     if not self.ws.closed:
         try:
             self.ws.send(json.dumps(err_message, separators=(', ', ':')))
         except WebSocketError as e:
             if is_ws_error_abnormal(e):
                 log.error('WebSocket fault: %s' % e.message,
                           extra=self.channel_history)
예제 #2
0
 def run_callback(self, obj, args=None):
     try:
         assert hasattr(self, 'on%s' % obj)
         f = getattr(self, 'on%s' % obj)
         if args is not None:
             return f(args)
         else:
             return f()
     except WebSocketError as e:
         if is_ws_error_abnormal(e):
             log.warn('WebSocket fault: %s' % e.message)
             log.error(format_exception(e, None),
                       extra=self.channel_history)
             report_exception(e, extra=self.channel_history)
     except Exception as e:
         self.onerror(e)
         if not self.ws.closed:
             try:
                 self.ws.close()
             except WebSocketError as e:
                 if is_ws_error_abnormal(e):
                     log.error('WebSocket fault: %s' % e.message,
                               extra=self.channel_history)
     finally:
         if USE_ALCHEMY_MW:
             session_handler.close()
 def run_callback(self, obj, args=None):
     try:
         assert hasattr(self, 'on%s' % obj)
         f = getattr(self, 'on%s' % obj)
         if args is not None:
             return f(args)
         else:
             return f()
     except WebSocketError as e:
         if is_ws_error_abnormal(e):
             log.warn('WebSocket fault: %s' % e.message)
             log.error(format_exception(e, None),
                       extra=self.channel_history)
             report_exception(e, extra=self.channel_history)
     except Exception as e:
         self.onerror(e)
         if not self.ws.closed:
             try:
                 self.ws.close()
             except WebSocketError as e:
                 if is_ws_error_abnormal(e):
                     log.error('WebSocket fault: %s' % e.message,
                               extra=self.channel_history)
     finally:
         if USE_ALCHEMY_MW:
             session_handler.close()
예제 #4
0
 def __call__(self):
     try:
         return self.callable()
     except Exception as e:
         log.error(format_exception(e, None))
         report_exception(e)
         data = {'error': 0, 'message': e.message}
         return XResponse(data)
예제 #5
0
def noodlesapp(env, start_response):
    """

    :rtype : noodles.http.Response
    :param env:
    :param start_response:
    :return: :rtype: :raise:
    """
    # Get request object
    if get_config('ENCODE_SEMICOLON_IN_REQUEST') is True:
        env['QUERY_STRING'] = re.sub('[;]', '%3b', env['QUERY_STRING'])
    request = Request(env)

    if "HTTP_X_FORWARDED_FOR" in env:
        x_forwarded_for = env["HTTP_X_FORWARDED_FOR"].split(',')[:1]
        if x_forwarded_for:
            request.remote_addr = x_forwarded_for[0]
    #print("Try to handle url_path '%s'" % request.path)
    # Get callable object with routine method to handle request
    producer = dispatcher.get_callable(request)
    if not producer:
        # May be here an error,raise exception
        raise Exception('Can\'t find callable for this url path')

    # Callable function must return Response object
    try:
        response = middlewares.run_chain(producer, request)
        if not hasattr(response, 'is_noodles_response'):
            response = producer()
    # Capture traceback here and send it if debug mode
    except Exception as e:
        f = Formatter()
        if EXCEPTION_FLAVOR=='html':
            traceback = '<pre>%s\n\n%s</pre>' \
                        % (json.dumps(env, indent=2, default=datahandler),
                           xml_escape(f.formatException(sys.exc_info())))
        else:
            traceback = '%s\n%s' \
                        % (json.dumps(env, indent=2, default=datahandler),
                           f.formatException(sys.exc_info()))
        extra = {'request': request}
        log.error(format_exception(e, None), extra=extra)
        report_exception(e, extra=extra)

        if DEBUG:
            response = Error500(e, traceback)
        else:
            response = Error500()
    finally:
        middlewares.end_chain(lambda x: x, request)

    return response(env, start_response)
예제 #6
0
def noodlesapp(env, start_response):
    """

    :rtype : noodles.http.Response
    :param env:
    :param start_response:
    :return: :rtype: :raise:
    """
    # Get request object
    if get_config('ENCODE_SEMICOLON_IN_REQUEST') is True:
        env['QUERY_STRING'] = re.sub('[;]', '%3b', env['QUERY_STRING'])
    request = Request(env)

    if "HTTP_X_FORWARDED_FOR" in env:
        x_forwarded_for = env["HTTP_X_FORWARDED_FOR"].split(',')[:1]
        if x_forwarded_for:
            request.remote_addr = x_forwarded_for[0]
    #print("Try to handle url_path '%s'" % request.path)
    # Get callable object with routine method to handle request
    producer = dispatcher.get_callable(request)
    if not producer:
        # May be here an error,raise exception
        raise Exception('Can\'t find callable for this url path')

    # Callable function must return Response object
    try:
        response = middlewares.run_chain(producer, request)
        if not hasattr(response, 'is_noodles_response'):
            response = producer()
    # Capture traceback here and send it if debug mode
    except Exception as e:
        f = Formatter()
        if EXCEPTION_FLAVOR=='html':
            traceback = '<pre>%s\n\n%s</pre>' \
                        % (json.dumps(env, indent=2, default=datahandler),
                           xml_escape(f.formatException(sys.exc_info())))
        else:
            traceback = '%s\n%s' \
                        % (json.dumps(env, indent=2, default=datahandler),
                           f.formatException(sys.exc_info()))
        extra = {'request': request}
        log.error(format_exception(e, None), extra=extra)
        report_exception(e, extra=extra)

        if DEBUG:
            response = Error500(e, traceback)
        else:
            response = Error500()
    finally:
        middlewares.end_chain(lambda x: x, request)

    return response(env, start_response)
예제 #7
0
 def __init__(self, data):
     if type(data) == dict:
         self.data = data
         return
     # TODO: check lib: https://bitbucket.org/Jeffrey/gevent-websocket/
     # issue/5/encoding-issue-when-sending-non-ascii
     self.raw_data = str(data.decode('utf-8')).encode(ENCODING)
     try:
         self.data = json.loads(self.raw_data)
     except ApiSocketError as e:
         self.data = self.raw_data
         extra = {'request': self.data}
         log.error(format_exception(e, None), extra=extra)
         report_exception(e, extra=extra)
 def __init__(self, data):
     if type(data) == dict:
         self.data = data
         return
     # TODO: check lib: https://bitbucket.org/Jeffrey/gevent-websocket/
     # issue/5/encoding-issue-when-sending-non-ascii
     self.raw_data = str(data.decode('utf-8')).encode(ENCODING)
     try:
         self.data = json.loads(self.raw_data)
     except ApiSocketError as e:
         self.data = self.raw_data
         extra = {'request': self.data}
         log.error(format_exception(e, None), extra=extra)
         report_exception(e, extra=extra)
예제 #9
0
 def onerror(self, e):
     """Send here Exception and traceback by Error channel
     """
     f = Formatter()
     history = json.dumps(self.channel_history,
                          indent=2,
                          default=datahandler)
     data = (
         history,
         xml_escape(f.formatException(sys.exc_info())),
     )
     if EXCEPTION_FLAVOR == 'html':
         traceback = '<pre>%s\n\n%s</pre>' % data
     else:
         traceback = '%s\n%s' % data
     if DEBUG:
         err_message = {
             'channel': WS_CHANNELS['ERROR_CHID'],
             'pkg': {
                 'exception': repr(e),
                 'tb': traceback
             }
         }
     else:
         err_message = {
             'channel': WS_CHANNELS['ERROR_CHID'],
             'pkg': {
                 'exception': 'error 500',
                 'tb': 'an error occurred'
             }
         }
     log.error(format_exception(e, None), extra=self.channel_history)
     report_exception(e, extra=self.channel_history)
     if not self.ws.closed:
         try:
             self.ws.send(json.dumps(err_message, separators=(', ', ':')))
         except WebSocketError as e:
             if is_ws_error_abnormal(e):
                 log.error('WebSocket fault: %s' % e.message,
                           extra=self.channel_history)