如何在python中创建自定义xmlrpc错误错误

时间:2013-01-21 16:23:01

标签: python xml-rpc xmlrpclib

在python中使用xmlrpclib时,客户端会将服务器端的错误报告为xmlrpclib.Fault。服务器端方法中的division by zero错误(使用SimpleXMLRPCServer)通常会提供如下输出:

xmlrpclib.Fault: <Fault 1: "<type 'exceptions.ZeroDivisionError'>:integer division or modulo by zero">

这很有用,因为它记录了错误的类型,但现在发生了。如何覆盖/修改xmlrpclib.Fault方法(SimpleXMLRPCServer?),以便报告错误,例如如下:

xmlrpclib.Fault: <Fault 1: "<type 'exceptions.ZeroDivisionError'>:integer division or modulo by zero MODULE: myMethod.py  LINE: 657">

即。包括出现错误的模块名称和行号。这可以在服务器端执行,而不会引发自定义异常吗?在客户端以相同的方式报告任何错误。

1 个答案:

答案 0 :(得分:3)

如果您使用SimpleXMLRPCServer,则可以覆盖内部_marshaled_dispatch方法,以便向生成的Fault()实例添加信息:

这是原始方法:

def _marshaled_dispatch(self, data, dispatch_method = None, path = None):
    try:
        params, method = xmlrpclib.loads(data)

        # generate response
        if dispatch_method is not None:
            response = dispatch_method(method, params)
        else:
            response = self._dispatch(method, params)
        # wrap response in a singleton tuple
        response = (response,)
        response = xmlrpclib.dumps(response, methodresponse=1,
                                   allow_none=self.allow_none, encoding=self.encoding)
    except:
        # report low level exception back to server
        # (each dispatcher should have handled their own
        # exceptions)
        exc_type, exc_value = sys.exc_info()[:2]
        response = xmlrpclib.dumps(
            xmlrpclib.Fault(1, "%s:%s" % (exc_type, exc_value)),
            encoding=self.encoding, allow_none=self.allow_none)
    return response

您可以继承SimpleXMLRPCServer.SimpleXMLRPCServer并覆盖此方法:

import SimpleXMLRPCServer
import sys
import xmlrbclib

class VerboseFaultXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
    def _marshaled_dispatch(self, data, dispatch_method = None, path = None):
        try:
            params, method = xmlrpclib.loads(data)

            # generate response
            if dispatch_method is not None:
                response = dispatch_method(method, params)
            else:
                response = self._dispatch(method, params)
            # wrap response in a singleton tuple
            response = (response,)
            response = xmlrpclib.dumps(response, methodresponse=1,
                                       allow_none=self.allow_none, encoding=self.encoding)
        except:
            # report low level exception back to server
            # (each dispatcher should have handled their own
            # exceptions)
            exc_type, exc_value, tb = sys.exc_info()
            while tb.tb_next is not None:
                tb = tb.tb_next  # find last frame of the traceback
            lineno = tb.tb_lineno
            code = tb.tb_frame.f_code
            filename = code.co_filename
            name = code.co_name
            response = xmlrpclib.dumps(
                xmlrpclib.Fault(1, "%s:%s FILENAME: %s LINE: %s NAME: %s" % (
                    exc_type, exc_value, filename, lineno, name)),
                encoding=self.encoding, allow_none=self.allow_none)
        return response

然后使用VerboseFaultXMLRPCServer代替SimpleXMLRPCServer.SimpleXMLRPCServer