Hey there list-
     i am trying to get my head around some exception handling. Could  
some kind soul please point me in the right direction?
Heres the code in my model:
    def self.fetch (page)
       Net::HTTP.start("192.168.0.2") do |http|
         data = http.get("/#{page}")
         data.body.gsub!(/\/temporaryimages/, "http://192.168.0.2/ 
temporaryimages")
         data.body.gsub!(/\/wrappers\/(\d+)\.news/i,
''/page/display/\1'')
       end
    end
What would be the proper way to wrap this in a  
begin..rescue...retry....end block so if the connection is reset or  
some othere http error occurs, the code will retry the fetch until it  
succeeds?
Thanks in advanced-
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org
On 7/8/05, Ezra Zygmuntowicz <ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org> wrote:> Hey there list- > i am trying to get my head around some exception handling. Could > some kind soul please point me in the right direction? > > Heres the code in my model: > def self.fetch (page) > Net::HTTP.start("192.168.0.2") do |http| > data = http.get("/#{page}") > data.body.gsub!(/\/temporaryimages/, "http://192.168.0.2/ > temporaryimages") > data.body.gsub!(/\/wrappers\/(\d+)\.news/i, ''/page/display/\1'') > end > end > > What would be the proper way to wrap this in a > begin..rescue...retry....end block so if the connection is reset or > some othere http error occurs, the code will retry the fetch until it > succeeds?Ezra- There is a pretty good example of this in the Net::HTTP documentation, which can be viewed at: http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html Basically you probably want to just do a recursive call to some method on the model. Something like this (borrowed straight from the docs) should help you out. def fetch( uri_str, limit = 10 ) # You should choose better exception. raise ArgumentError, ''HTTP redirect too deep'' if limit == 0 response = Net::HTTP.get_response(URI.parse(uri_str)) case response when Net::HTTPSuccess then response when Net::HTTPRedirection then fetch(response[''location''], limit - 1) else response.error! end end Cheers, Ben
On 7/8/05, Ben Schumacher <benschumacher-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> def fetch( uri_str, limit = 10 ) > # You should choose better exception. > raise ArgumentError, ''HTTP redirect too deep'' if limit == 0 > > response = Net::HTTP.get_response(URI.parse(uri_str)) > case response > when Net::HTTPSuccess then response > when Net::HTTPRedirection then fetch(response[''location''], limit - 1) > else > response.error! > end > end... I suppose I should note that this uses get_response, so it doesn''t expose the exceptions and therefore saves you from have to rescue -- you just have to deal with the response object. Cheers, bs.