在我的 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
问题是我很多时候都在本地环境中运行我的应用程序。当没有可用的互联网连接时(这种情况在我身上经常发生,不要问为什么),我收到以下错误:
SocketError. Failed to open TCP connection to api.facebook.com:443 (getaddrinfo: nodename nor servname provided, or not known)
如何避免离线?
我检查了RestClient documentation已经但无济于事。感谢您的帮助。
最佳答案
您可以使用毯子rescue => e
并使用byebug来检查引发的异常吗?如果在开发环境中,您可以挽救该错误。
这里提到了一些其他选项 Check if Internet Connection Exists with Ruby?
关于ruby - 当没有网络可用时,如何在开发模式下救援 RestClient API 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50436551/