在 Ruby 中捕获外部 URL 错误
我的脚本的一部分联系 Twitter 的 api 来获取分享数量:
twitter = JSON.parse(open("http://urls.api.twitter.com/1/urls/count.jsonurl=TheURL).read)
我担心的是,如果 Twitter 宕机或运行缓慢,它会阻止我的网站工作,因为会返回错误。
如何捕获错误,甚至设置一个较短的超时,以便在调用时间过长时可以跳过该调用?
谢谢, 汤姆
Part of my script contacts Twitters api to get the number of shares:
twitter = JSON.parse(open("http://urls.api.twitter.com/1/urls/count.jsonurl=TheURL).read)
My concern is if Twitter is down or slow, it will stop my site from working as an error is returned.
How can I catch the error or even set a short timeout so I can skip the call if it's taking too long?
Thanks,
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 AJAX 请求中连接到 Twitter 可能是个好主意。如果你想在几秒钟后超时,那么你有 Timeout 模块。
It can be a good idea to connect to Twitter in AJAX request. If you want to get timeout after some seconds then you have Timeout module in Ruby.