How to raise Suds.WebFault from python code?
Asked Answered
M

3

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.

Macneil answered 13/4, 2011 at 15:19 Comment(1)
The answer by chrissygormley works for me. If it works for you, please checkmark it.Subdebutante
W
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
Wraparound answered 15/4, 2011 at 15:54 Comment(2)
Not at all what's being asked for.Diagraph
This actually is how you catch a WebFault, not how you throw/raise one.Situs
M
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)
Mazurka answered 11/8, 2016 at 11:15 Comment(1)
Great answer thanks. For reference I need to do this in a mock system to emulate a generic server processing error.Magen
R
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.

Regicide answered 16/3, 2016 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.