Pierre Valade
Pierre Valade

Reputation: 3409

rescue Timeouts with SystemTimer

I'm using the SystemTimer gem to deal with timeout problems. https://github.com/ph7/system-timer

I can't find a way to catch the Exception when a Timeout

begin
  SystemTimer.timeout_after(10.seconds) do
    # facebook api
    rest_graph.fql(query)
  end
rescue RestGraph::Error::InvalidAccessToken
  return nil
rescue Timeout::Error
  # never executed
end

But the last Exception Timeout::Error is never triggered.

Upvotes: 1

Views: 704

Answers (2)

the Tin Man
the Tin Man

Reputation: 160551

Why not use Timeout, which comes with 1.9.2 and is designed to do this?

require 'timeout'
status = Timeout::timeout(5) {
  # Something that should be interrupted if it takes too much time...
}

Upvotes: 1

Guilherme Bernal
Guilherme Bernal

Reputation: 8293

Try this: (based on your link)

class TimedOut < StandardError
end

begin
  SystemTimer.timeout_after(10.seconds, TimedOut) do
    # ...
  end
rescue TimedOut
  # ...
end

Upvotes: 0

Related Questions