在我的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:
提供或不知道nodename或servname)
我怎样才能摆脱离线状态?
我已经查过了,但没有用。谢谢你的帮助。
最佳答案
你能用毯子rescue => e
和byebug来检查引发的异常吗?如果是在开发环境中,您就可以挽救这个错误。
这里还有一些其他选项Check if Internet Connection Exists with Ruby?