Exemple #1
0
    def advance(self, buffer):
        """
        Advance the loop for this client
        Params:
            buffer -- data buffer to be parsed
        """
        try:
            if constants.server_ready:
                function_list = message.List("function", message.Function)
                functions = self.parse(buffer)
                if functions is None:
                    return
                if isinstance(functions, enums.ServerCommand):
                    if functions == enums.ServerCommand.RequestAuth:
                        self.handshake()
                    return functions

                for func, func_args, ctx in functions:
                    log.d("Calling function", func, "with args", func_args)
                    func_msg = message.Function(func.__name__)
                    try:
                        if ctx:
                            func_args['ctx'] = self.context
                            msg = func(**func_args)
                        else:
                            msg = func(**func_args)
                        assert isinstance(msg, message.CoreMessage) or None
                        func_msg.set_data(msg)
                    except exceptions.CoreError as e:
                        func_msg.set_error(message.Error(e.code, e.msg))
                    function_list.append(func_msg)

                # bad functions
                for fname, e in self.errors:
                    function_list.append(
                        message.Function(fname,
                                         error=message.Error(e.code, e.msg)))
                self.errors.clear()

                self.send(function_list.serialize(session_id=self.session.id))
            else:
                self.on_wait()
        except exceptions.CoreError as e:
            log.d("Sending exception to client:", e)
            self.on_error(e)

        except BaseException:
            if not constants.dev:
                log.exception("An unknown critical error has occurred")
                self.on_error(
                    exceptions.HappypandaError(
                        "An unknown critical error has occurred")
                )  # TODO: include traceback
            else:
                raise
Exemple #2
0
 def on_error(self, exception):
     """
     Creates and sends error message to client
     """
     assert isinstance(exception, exceptions.CoreError)
     e = message.Error(exception.code, exception.msg)
     s_id = self.session.id if self.session else ""
     self.send(
         message.finalize(None,
                          error=e.json_friendly(False),
                          session_id=s_id))
Exemple #3
0
    def advance(self, buffer):
        """
        Advance the loop for this client
        Params:
            buffer -- data buffer to be parsed
        """
        with db.cleanup_session():
            try:
                if constants.server_ready:
                    try:
                        buffer = gzip.decompress(buffer)
                    except (zlib.error, OSError) as e:
                        raise exceptions.ParsingError(utils.this_function(),
                                                      str(e))

                    function_list = message.List("function", message.Function)
                    functions = self.parse(buffer)
                    if functions is None:
                        return
                    if isinstance(functions, enums.ServerCommand):
                        return functions

                    for func, func_args in functions:
                        log.d("Calling function", func, "with args", func_args)
                        func_msg = message.Function(func.__name__)
                        try:
                            msg = func(**func_args)
                            assert isinstance(
                                msg, message.CoreMessage) or msg is None
                            func_msg.set_data(msg)
                        except exceptions.CoreError as e:
                            log.w(f"Error on func '{func.__name__}':", e)
                            func_msg.set_error(message.Error(e.code, e.msg))
                        function_list.append(func_msg)

                    # bad functions
                    for fname, e in self.errors:
                        function_list.append(
                            message.Function(fname,
                                             error=message.Error(
                                                 e.code, e.msg)))
                    self.errors.clear()

                    self.send(
                        function_list.serialize(session_id=self.session.id))
                else:
                    self.on_wait()
            except exceptions.CoreError as e:
                log.w("Sending exception to client:", e)
                self.on_error(e)

            except Exception as e:
                if not constants.dev:
                    log.exception("An unhandled critical error has occurred")
                    if isinstance(e, (PermissionError, FileNotFoundError,
                                      NotImplementedError)):
                        self.on_error(exceptions.HappypandaError(str(e)))
                    else:
                        self.on_error(
                            exceptions.HappypandaError(
                                f"An unhandled critical error has occurred: {e.__class__.__name__}"
                            ))
                else:
                    log.exception("An unhandled critical error has occurred")
                    raise