Crusader633
Crusader633

Reputation: 595

Savon - Program crash at "Error" Response

I made a simple SOAP-Request using Savon.

require 'savon'


client = Savon.client(:wsdl => 'https://url?wsdl', :ssl_verify_mode => :none, 
                    wsse_auth: ["username", "password"],pretty_print_xml: true)
trackingnumber = 'anyNumber'


response = client.call(:shipment_status) do
  message(Id: trackingnumber)
end


puts response

The problem is, if I get a "Error-Response", for instance if the trackingnumber don`t exists, then my program is crashing. But I dont want the program to crash, I want to keep the response. Here is the error:

c:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.3.2/lib/savon/response.rb:85:in `raise_soap_and_http_errors!': (soap:Server) Fault occurred while processing. (Savon::SOAPFault)
from c:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.3.2/lib/savon/response.rb:14:in `initialize'
from c:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.3.2/lib/savon/operation.rb:64:in `new'
from c:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.3.2/lib/savon/operation.rb:64:in `create_response'
from c:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.3.2/lib/savon/operation.rb:55:in `call'
from c:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.3.2/lib/savon/client.rb:36:in `call'

Is there a solution for this problem?

Upvotes: 0

Views: 1562

Answers (1)

Steffen Roller
Steffen Roller

Reputation: 3494

I think your syntax is off. Do you use Savon 2.x?

You should guard the call with an Exception handler.

require 'savon'

client = Savon.client(
  :wsdl => 'https://url?wsdl',
  :ssl_verify_mode => :none,
  wsse_auth: ["username", "password"],
  :raise_errors => true, # false if you don't want to see exceptions
  pretty_print_xml: true)

trackingnumber = 'anyNumber'

begin
  response = client.call(
    :shipment_status,
    :messageId => {:trackingnumber => trackingnumber}
  )
rescue Savon::Error => soap_fault
  print "Error: #{soap_fault}\n"
end

puts response

You can also set the parameter :raise_errors to false when creating your client. then you have to handle the return values accordingly.

Upvotes: 3

Related Questions