简体   繁体   中英

How to rescue ruby callbacks?

Is there a simple way to wrap ruby/rails with begin rescue blocks?

We're implementing the search functionality in our webapp, using ElasticSearch + Tire. Tire uses callbacks on models that we want to index on the ES server. Sometimes these callbacks fail for some reason.

I want to be able to rescue these errors - Is there a simple way of doing that?

Without you providing more detail, what about something like this

class Model < ActiveRecord::Base

    after_save :my_callback

    def my_callback
        begin
          # do the thing you want
        rescue
          # callback failed - raise error or whetever
        end
    end

end

我将查看http://mrchrisadams.tumblr.com/post/333036266/catching-errors-in-rails-with-rescue-from ,其中讨论了rescue_from功能以及如何在整个应用程序中使用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM