Savon - 程序崩溃"错误"响应

时间:2014-03-14 17:07:22

标签: ruby wsdl savon

我使用Savon做了一个简单的SOAP-Request。

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

问题是,如果我收到“错误响应”,例如如果跟踪号不存在,那么我的程序就会崩溃。但我不希望程序崩溃,我想保持响应。这是错误:

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'

这个问题有解决方案吗?

1 个答案:

答案 0 :(得分:3)

我认为您的语法已关闭。你使用Savon 2.x吗?

您应该使用异常处理程序保护调用。

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

您还可以在创建客户端时将参数:raise_errors设置为false。那么你必须相应地处理返回值。