Class: Saddle::Middleware::RubyTimeout

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/saddle/middleware/ruby_timeout.rb

Overview

Public: Enforces a ruby timeout on the request and throws one consistent exception for all classes of timeout, internal or from faraday. :timeout must be present in the request or client options

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/saddle/middleware/ruby_timeout.rb', line 15

def call(env)
  timeout = env[:request][:hard_timeout] # nil or 0 means no timeout
  Timeout.timeout(timeout, Saddle::TimeoutError) do
    @app.call(env)
  end
# It is possible that faraday will catch the timeout first and throw
# this exception, rethrow as a class derived from standard error.
rescue Faraday::Error::TimeoutError
  raise Saddle::TimeoutError
end