6

I am trying to raise a Suds.WebFault from python code. The __init__ method\constructor takes three arguments __init__(self, fault, document). The fault has fault.faultcode and fault.detail members\attributes\properties. I could not find out what class fault belongs to no matte what I tried. How do I raise Suds.WebFault type exception from python code?

Thanks in advance.

joaquin
  • 82,968
  • 29
  • 138
  • 152
neel
  • 61
  • 1
  • 2

3 Answers3

6

Not sure what exactly you are asking but you can throw a web fault using:

import suds

try:
    client.service.Method(parameter)
except suds.WebFault, e:
    print e
chrisg
  • 40,337
  • 38
  • 86
  • 107
3

WebFault is defined in suds.__init__.py as:

class WebFault(Exception):
    def __init__(self, fault, document):
        if hasattr(fault, 'faultstring'):
            Exception.__init__(self, u"Server raised fault: '%s'" %
                fault.faultstring)
        self.fault = fault
        self.document = document

Therefore to raise a WebFault with a meaningful message you will need to pass an object as the first param with the message. Document can simply be None unless required.

import suds

class Fault(object): 
    faultstring = 'my error message'

raise suds.WebFault(Fault(), document=None)
igniteflow
  • 8,404
  • 10
  • 38
  • 46
0

WebFault is only meant to be actually raised when a <Fault> element is returned by the web server. So it's probably a bad idea to raise that yourself.

If you still would like to, I would start looking at the code where the framework raises it: https://github.com/unomena/suds/blob/4e90361e13fb3d74915eafc1f3a481400c798e0e/suds/bindings/binding.py#L182 - and work backwards from there.

Emil Stenström
  • 13,329
  • 8
  • 53
  • 75