mvantellingen / python-zeep

A Python SOAP client
http://docs.python-zeep.org
Other
1.88k stars 586 forks source link

how to read exception details that are present in exception xml but not in zeep.exceptions.fault object #1314

Open andreabisello opened 2 years ago

andreabisello commented 2 years ago

maybe simillar to https://github.com/mvantellingen/python-zeep/issues/392, my call returns a detail with the error (this is the debug xml)

zeep.transports: HTTP Post to https://testsdc2csolution.solutiondocondemand.com/Basic/RepositoryServiceAdmin.svc:
<?xml version='1.0' encoding='utf-8'?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>http://tempuri.org/IRepositoryServiceAdmin/deleteLavoro</wsa:Action><wsa:MessageID>urn:uuid:c7b7cbdf-51e1-4958-89ad-1ec201aa10a0</wsa:MessageID><wsa:To>https://testsdc2csolution.solutiondocondemand.com/Basic/RepositoryServiceAdmin.svc</wsa:To><wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken><wsse:Username>XXXX</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXX</wsse:Password></wsse:UsernameToken></wsse:Security></soap-env:Header><soap-env:Body><ns0:deleteLavoro xmlns:ns0="http://tempuri.org/"><ns0:id>629782ea8d1ebd02dfaa5c3a</ns0:id></ns0:deleteLavoro></soap-env:Body></soap-env:Envelope>
zeep.transports: HTTP Response from https://testsdc2csolution.solutiondocondemand.com/Basic/RepositoryServiceAdmin.svc (status: 500):
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>2022-06-01T15:16:58.673Z</u:Created><u:Expires>2022-06-01T15:21:58.673Z</u:Expires></u:Timestamp></o:Security></s:Header><s:Body><s:Fault><faultcode xmlns:a="http://schemas.microsoft.com/2009/WebFault">a:Conflict</faultcode><faultstring xml:lang="en-US">Conflict</faultstring><detail><RepositoryServiceAdmin.ErrorData xmlns="http://schemas.datacontract.org/2004/07/WcfConservazione" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DetailedInformation i:nil="true"/><Reason>Non è possibile eliminare il lavoro in quanto presenta 1 PdV da conservare</Reason></RepositoryServiceAdmin.ErrorData></detail></s:Fault></s:Body></s:Envelope>

the details is

<detail><RepositoryServiceAdmin.ErrorData xmlns="http://schemas.datacontract.org/2004/07/WcfConservazione" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><DetailedInformation i:nil="true"/><Reason>Non è possibile eliminare il lavoro in quanto presenta 1 PdV da conservare</Reason></RepositoryServiceAdmin.ErrorData></detail>

but i'm not able to access from returned exception

image

how to access it?

thanks

andreabisello commented 2 years ago

in the meanwhile i used the "raw" mode of zeep to read the response of the exception. https://docs.python-zeep.org/en/master/settings.html#context-manager

ajendrex commented 1 year ago

I was in similar situation and needed to have programmatic access to the error for good handling.

The solution for OP's question could be something like:

import zeep

history = zeep.plugins.HistoryPlugin()
client = zeep.Client(wsdl, plugins=[history])
...

try:
    client.service.YourMethod()
except zeep.exceptions.Fault:
    envelope = history.last_received["envelope"]
    error_text = envelope.find(".//{*}RepositoryServiceAdmin.ErrorData").text