需要帮助处理Timeon :: Savon中的错误

时间:2019-05-09 09:53:28

标签: ruby savon

我正在Ruby中建立REST-API和SOAP API之间的连接(没有Rails)。 对于SOAP调用,我使用Savon gem,这很棒。

但是,我无法从docs中弄清楚,Savon如何处理Timeout::Error

它会引发Savon::HTTPErrorSavon::SOAPFault吗?

请告知。

2 个答案:

答案 0 :(得分:1)

我自己很好奇。经过一些实验并浏览了Savon的源代码之后,似乎并没有处理传输级错误并将其转换为Savon自己的异常类型,而是按原样抛出,因此,如果需要处理它们,则必须处理抛出的异常由基础的HTTP客户端库提供。

请务必注意,Savon通过httpi抽象层支持多个HTTP客户端。默认情况下,它仅从可用的选项中选择一个,但是如果您需要处理它的异常,则不应该依赖自动选择,而应明确配置应使用的HTTPI适配器(例如HTTPI.adapter = :net_http)。

下面的代码可用于通过您选择的HTTPI适配器测试超时情况。

实验代码

服务器(用PHP编写,因为there are no up-to-date working solutions for writing a dead-simple SOAP server like this, without a ton of boilerplate code, in Ruby):

<?php
// simple SOAP server - run with 'php -S localhost:12312 name_of_this_file.php'
class SleepySoapServer
{
    public function hello()
    {
        sleep(3600); // take an hour's nap before responding

        return 'Hello, world!';
    }
}

$options = array('uri' => 'http://localhost:12312/');
$server = new SoapServer(null, $options);
$server->setClass(SleepySoapServer::class);
$server->handle();

客户端(使用Savon 2):

require 'savon'

HTTPI.adapter = :net_http # request Net::HTTP client from the standard library

uri = 'http://localhost:12312'
client = Savon.client do
  endpoint uri
  namespace uri
end

response = client.call :Hello
p response.body

答案 1 :(得分:0)

如果您不希望挽救错误,可以通过以下方法告诉Savon不要提出错误

Savon.configure do |config|
  config.raise_errors = false 
end
相关问题