代码之家  ›  专栏  ›  技术社区  ›  Tintin81

在没有Internet可用的情况下,如何在开发模式下挽救restclientAPI调用?

  •  2
  • Tintin81  · 技术社区  · 8 年前

    在我的Rails应用程序中,我正在进行各种API调用,如下所示:

    class SearchFacebook
    
      FALLBACK_STARS = 5.0
    
      def self.call
        begin
          url = "https://graph.facebook.com/v2.12/#{FACEBOOK_PAGE_ID}"
          response = RestClient.get(url, :params => {:access_token => FACEBOOK_ACCESS_TOKEN, :fields => "overall_star_rating"})
          json = JSON.parse(response)
          stars = json["overall_star_rating"]
        rescue RestClient::ExceptionWithResponse => error
          stars = FALLBACK_STARS
        end
        {:stars => stars}
      end
    
    end
    

    问题是我经常在本地环境中运行我的应用程序。当没有可用的Internet连接时(这种情况在我身上经常发生,不要问为什么),我会得到以下错误:

    短袜店 . 无法打开到api.facebook.com:443的TCP连接(getaddrinfo:

    离线

    RestClient documentation 已经没用了。谢谢你的帮助。

    2 回复  |  直到 8 年前
        1
  •  2
  •   Michael Brawn matt    8 年前

    你能用条毯子吗 rescue => e

    这里还有一些其他的选择 Check if Internet Connection Exists with Ruby?

        2
  •  0
  •   Tintin81    8 年前

    好吧,我解决了这个问题 SocketError :

    rescue SocketError, RestClient::ExceptionWithResponse => error
    

    this answer 很有帮助。