Handling exceptions

I’m using a web service in Ruby script and occasionally I get errors. I
want to add error handling code, but do not know how to describe the
error to Ruby. Here’s the error:

GeoNames::APIError: {“message”=>“ERROR: canceling statement due to
statement timeout”, “value”=>13}

What do I put after “rescue”? I can’t leave it blank because I have more
than one error to capture.

Service is geonames.org BTW.

Another error I have to capture is:

GeoNames::APIError: {“message”=>“we are afraid we could not find a
neighbourhood for latitude and longitude :33.793038,-118.327683”,
“value”=>15}

Thanks.

PS Preview isn’t working. I didn’t see any returns/paragraphs in the
Preview

If it’s the same error class you’d have to use the same rescue and then
test the error content after rescuing in order to deal with it
correctly.

something like this

begin
#Code
rescue GeoNames::APIError => err
case err.message
when /timeout/
# Handle ‘timeout’ error
when /could not find/
# Handle ‘could not find’ error
else
# Unhandled error
raise err
end
end

Thank you. I didn’t understand the syntax.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs