user984621
user984621

Reputation: 48483

Trying to connect Google, but sometimes "Connection failed"

When I try to fetch contacts from Google, but sometimes when I click on the link to connect with Google API, I get an error page, where is written that Connection Failed or an Timeout Issue. When I try it like in another minute after this errors, it's usually working, but is there any way to avoid of this error?

It's not very user friendly to display just this error message to users...

EDIT: A little bit of code

  begin
    @contacts = Contacts::Gmail.new(@email, @password)
  rescue
    @error_message = 'Incorrect password. Try it, please, again.'
  end

This is basically the most important part - when I call this action, the command

@contacts = Contacts::Gmail.new(@email, @password)

will try to connect Google's API. The problem is, that sometimes it fail with error messages described about - it's like in 1 case from 5 attempts.

Is there any way to handle this situation?

Upvotes: 0

Views: 104

Answers (1)

Mike Szyndel
Mike Szyndel

Reputation: 10592

I think you may use retry

begin
  @contacts = Contacts::Gmail.new(@email, @password)
rescue Timeuot, ConnectionFailed
  retry
rescue
  @error_message = 'Incorrect password. Try it, please, again.'
end

You need to put real exception classes in place of Timeuot & ConnectionFailed. This is a little bit risky because if you get a permanent error it may end up in infinite loop, so it would be wise to have some kind of counter and retry only 3 times for example.

For more detailed info check out docs http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html

Upvotes: 1

Related Questions