0

I have 3 servers running beside my app and I am sending RESTful request to them, but I want to have a loop that tries one of these three until it gets accurate response. In my execution when it gets Bad Respond from first server program raises "BadRespondError" and I cannot rescue it in my codes because execution goes to rescue.rb in "actionpack" gem. How could I solve that problem? Can I solve it without working on Execeptions and Errors just with a simple loop or if it is not possible, how could I rescue BadRespondError in my code other than rescue.rb

Here are my code Requesting ....

@response = carrot.dcs_request(uri, {
       "dcs.source" => "etools",
       "query" => @query,
       "dcs.output.format" => "JSON",
       "dcs.clusters.only" => "false"
      })
@json = @response
@response = JSON.parse(@response)

....

Request function

def dcs_request(uri, data)

    boundary = Array::new(16) { "%2.2d" % rand(99) }.join()
    extheader = {
      "content-type" => "multipart/form-data; boundary=___#{ boundary }___"
    }
    client = HTTPClient.new
    response = client.post_content(uri, data, extheader)
  end

So think like uri as an array and I want to loop on it until I get accurate response from one of them.

Eren Golge
  • 802
  • 2
  • 12
  • 27
  • I assume the BadResponseError happens in the post_content. What does post_content do when there is a bad response? Does it raise BadResponseError? If so, just rescue it, as Pasted suggested. – Marlin Pierce May 08 '12 at 11:26
  • Can you post the full stack trace? – Pasted May 08 '12 at 20:58

1 Answers1

0

You could try a begin..rescue clause with a specific error (BadResponseError) Somethng like,

 begin

 rescue Net::IMAP::BadResponseError, Net::IMAP::NoResponseError => e

 end

Shouldn't the rescue clause in the app code be run before Rails raise the error to ActionPack's error handling? (maybe I don't understand the question correctly)

Begin, Rescue and Ensure in Ruby?

Community
  • 1
  • 1
Pasted
  • 864
  • 1
  • 12
  • 22