我如何在Ruby on Rails代码中捕获Zlib :: BufError? [英] How can I catch Zlib::BufError in my Ruby on Rails code?

查看:118
本文介绍了我如何在Ruby on Rails代码中捕获Zlib :: BufError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Rails构建了一个应用程序。我有一个很大的地址列表,我通过使用Geocoder的Bing / Google API进行标准化。这是我目前的电话来获取adddress信息:

  Geocoder.search(address)

这个工作非常好,我可以解析JSON的响应。但是当我离开脚本运行时,有时会出现以下错误:

  Zlib :: BufError:缓冲区错误

而不是脚本掉下来,我想抓住这个错误并继续执行脚本。我怎么能用Ruby呢?

解决方案

确保 Zlib 可用,您需要在要获取错误的文件中 require'zlib'。它不像 Geocoder 已经声明为任何地方的依赖项,所以当您的拯救行正在评估。

  require'zlib'

begin
Geocoder.search ('110 William St,28th New York,NY 10038')
rescue Zlib :: BufError =>
put'Stack Overflow似乎已经移动'
end


I have built an app with Rails. I have a large list of addresses which I am standardizing via the Bing/Google API using Geocoder. This is my current call to get the adddress info:

Geocoder.search(address)

This works perfectly fine and I can parse the response JSON. However when I leave the script running it sometimes falls over with the following error:

Zlib::BufError: buffer error

Instead of the script falling over I would like to catch this error and continue with the script. How could I do this with Ruby? I am not sure which exception to catch, if any.

解决方案

To ensure Zlib is available, you need to require 'zlib' in the file where you want to catch the error. It doesn't look like Geocoder has declared that as a dependency itself anywhere, so it may not be loaded when your rescue line is being evaluated.

require 'zlib'

begin
  Geocoder.search('110 William St, 28th Floor New York, NY 10038')
rescue Zlib::BufError => boom
  puts 'Stack Overflow appears to have moved.'
end

这篇关于我如何在Ruby on Rails代码中捕获Zlib :: BufError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆