0

I'm building a connection between REST-API and SOAP API in Ruby (without Rails). For SOAP calls I use Savon gem, which is great.

However, I cannot figure out from the docs, how does Savon handle Timeout::Error?

Does it raise Savon::HTTPError or Savon::SOAPFault?

Please advise.

Dende
  • 545
  • 6
  • 19

2 Answers2

1

I was curious myself. After a bit of experimentation and skimming through Savon sources it seems that transport-level errors aren't handled and translated to Savon's own exception types, but thrown "as is", so if you need to handle them, you have to handle exceptions thrown by the underlying HTTP client library.

It's important to note that Savon supports multiple HTTP clients through the httpi abstraction layer. By default it just chooses one from those being available, but if you need to handle it's exceptions, you shouldn't rely on the automatic selection, but explicitly configure which HTTPI adapter should be used (e.g. HTTPI.adapter = :net_http).

The code below can be used to test the timeout scenario with HTTPI adapter of your choice.

Code for experimentation

Server (written in PHP, because 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();

Client (using 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
igneus
  • 963
  • 10
  • 25
  • Thank you for your answer. I was also experimenting, and in the test simulating Timeout the Savon client was throwing a `Savon::HTTPError` with code 408. So, I decided to handle Timeout as a `Savon::HTTPError`. – Dende May 14 '19 at 08:31
  • @Dende which Savon version are you using? It seems to be older than 0.8.0 (released 2010), where HTTPI was introduced. – igneus May 14 '19 at 11:25
  • `savon (2.12.0) akami (~> 1.2) builder (>= 2.1.2) gyoku (~> 1.2) httpi (~> 2.3) nokogiri (>= 1.8.1) nori (~> 2.4) wasabi (~> 3.4)` – Dende May 14 '19 at 14:43
0

If you don’t like to rescue errors, here’s how you can tell Savon not to raise them

Savon.configure do |config|
  config.raise_errors = false 
end
igneus
  • 963
  • 10
  • 25
Thi403
  • 1
  • 6