【问题标题】:Trying to connect Google, but sometimes "Connection failed"尝试连接 Google,但有时“连接失败”
【发布时间】:2013-07-20 10:36:33
【问题描述】:

当我尝试从 Google 获取联系人时,但有时当我单击链接以连接 Google API 时,我收到一个错误页面,其中写有 Connection Failed 或 超时问题。 当我在这个错误发生后的另一分钟尝试它时,它通常可以工作,但是有什么办法可以避免这个错误?

仅向用户显示此错误消息对用户不太友好...

编辑:一点代码

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

这基本上是最重要的部分——当我调用这个动作时,命令

@contacts = 联系人::Gmail.new(@email, @password)

将尝试连接 Google 的 API。问题是,有时它会因错误消息而失败 - 就像 5 次尝试中的 1 次一样。

有没有办法处理这种情况?

【问题讨论】:

  • 也许你提供一些细节?
  • 没有一些代码有点难以支持你
  • 我已经添加了一些代码,但它确实是基本和简单的东西,基本上只有这一行调用Google API。
  • 一个想法是重试此类异常

标签: ruby-on-rails ruby api gmail


【解决方案1】:

我想你可以使用retry

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

您需要使用真正的异常类来代替 Timeuot 和 ConnectionFailed。 这有点冒险,因为如果您遇到永久性错误,它可能会陷入无限循环,因此最好有某种计数器并仅重试 3 次。

有关更多详细信息,请查看文档http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2021-10-21
    • 1970-01-01
    • 2014-09-18
    相关资源
    最近更新 更多