如何解析来自zeep python库的soap.detail(lxml.etree._Element)肥皂

时间:2017-03-21 04:58:28

标签: python soap zeep

当我用zeep调用soap服务时,我正在尝试获取错误详细信息。

如何解析zeep.exceptions.Fault.detail?它返回lxml.etree._Element。

我正在使用此代码:

try:
    client = Client(wsdl=self.__wsdl)
    response = client.service.CustomerInformation(CustomerInformationService=self.service, faultStyle='wsdl')
except Fault as error:
    detail = error.detail
    # parse detail here

这是响应XML:

<?xml version="1.0" ?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Body >
        <soap-env:Fault  >
            <faultcode>soap-env:Client</faultcode>
            <faultstring>Client Error</faultstring>
            <detail>
                <ouaf:Fault xmlns:ouaf="urn:oracle:ouaf">
                    <ResponseStatus>F</ResponseStatus>
                    <ResponseCode>2013</ResponseCode>
                    <ResponseText>
                            Error while executing the request:
                            (Server Message)
                                Category: 90006
                                Number: 32200
                                Call Sequence: 
                                Program Name: CustomerInformationService
                                Text: The personal account was not found: 9134211141
                                Description:  
                                Table: null
                                Field: null
                    </ResponseText>
                    <ResponseData numParm="1"  text="The personal account was not found: 9134211141"  category="90006"  number="32200"  parm1="9134211141"  />
                </ouaf:Fault>
            </detail>
        </soap-env:Fault>
    </soap-env:Body >
</soap-env:Envelope>

错误的定义&#39;我的wsdl中存在来自xml数据的类型。

2 个答案:

答案 0 :(得分:1)

您可以使用error.codeerror.message来匹配您要查找的错误。

https://github.com/mvantellingen/python-zeep/blob/master/src/zeep/exceptions.py#L53

如果在error.detail中看不到任何内容,请考虑向python-zeep项目发送PR。

答案 1 :(得分:0)

我知道这是一个老问题,但是寻找答案使我到了这里,现在我也知道该怎么做。

示例中wsdl的URL以及凭据组成。

import zeep

url_to_wsdl = 'www.some_SOAP_site.com/soap?wsdl'

client = zeep.Client(url_to_wsdl)

credentials = {
    'login' : 'my_login',
    'pass' : 'my_pass'
}

my_query = "SELECT COLUMN1 FROM TABLE1"

try:
    client.service.query(my_query)
except zeep.exceptions.Fault as fault:
    parsed_fault_detail = client.wsdl.types.deserialize(fault.detail[0])

print(parsed_fault_detail)

结果

{
    'errorCode': 'INVALID_SESSION',
    'errorMessage': 'Invalid session!'
}

请不要忘记[0]之后的fault.detail,并尝试将其递增以查看是否有更多错误详细信息。