How do I get the error code and description from Savon :: SOAPFault?

In the Savon log I see that my SOAP errors contain XML like this:

<errorCode>666</errorCode><errorDescription>some evil error</errorDescription>

      

Does anyone know how to parse the error code and description from the answer? Sorry if this is a stupid question, but I've tried everything and I couldn't find any documentation on the matter.

+2


a source to share


3 answers


For the record, the only way I have been able to do this is to disable Savon exceptions:

Savon::Response.raise_errors = false

      



After that, I had to check response.soap_fault? after every SOAP call to see if there was an error. Then I could access the error data using response.to_hash.

+1


a source


I believe you are looking for this:

def your_method(credentials)
  # your client call here
rescue Savon::SOAPFault => error
  fault_code = error.to_hash[:fault][:faultcode]
  raise CustomError, fault_code
end

      



Obtained this solution from the Savon

documentation .

Thanks!

+1


a source


I am using this patch:

module Savon
  class SOAPFault

    def soap_error_code
      fault = nori.find(to_hash, 'Fault')
      if nori.find(fault, 'faultcode')
        nori.find(fault, 'faultcode').to_i
      elsif nori.find(fault, 'Code')
        nori.find(fault, 'Code', 'Value').to_i
      end
    end

  end
end

      

Then in the controller:

begin
 # do something
rescue Savon::SOAPFault => e
  raise CustomError, e.soap_error_code
end

      

0


a source







All Articles