1

我正在遍历一组用户,作为其中的一部分,我正在调用第三方 API(通过Intercom API Ruby 包装器)。

Intercom::ResourceNotFound如果找不到用户,Intercom API 会抛出一个错误,这会停止整个过程。

如果找不到它,我只想让它跳过用户。

User.each do |user|
    user = Intercom::User.find_by_email(user.email) # Intercom::ResourceNotFound thrown if not found
    user.custom_data["Example"] = true
    user.save
end

这是 Intercom Ruby 包装器的问题吗?或者是否有典型的 Ruby 或 Rails 方式来处理这类事情?

4

1 回答 1

3

只是捕捉异常怎么样?

User.each do |user|
  begin
   user = Intercom::User.find_by_email(user.email) # Intercom::ResourceNotFound thrown if not found
   user.custom_data["Example"] = true
   user.save
  rescue Intercom::ResourceNotFound
  end
end

由于您只想在找不到用户时跳过该用户(并引发异常),因此rescue. 但是如果你想放一些调试信息或类似的东西,你可以写:

  rescue Intercom::Resource
    puts %{Could not work on user...}
  end
于 2012-08-09T11:36:14.540 回答