在 sidekiq_batch.rb 中,
def sidekiq_status
begin
something
rescue => e
Rails.logger.error("\nRESCUENIL in sidekiq_status #{e.class} #{e.message} in #{e.backtrace}")
# FIXME RESCUENIL
nil
end
end
在 checkin.rb 中,
def attached_receipt_image
begin
something else
rescue => e
Rails.logger.error("\nRESCUENIL in attached_receipt_image #{e.class} #{e.message} in #{e.backtrace}")
# FIXME RESCUENIL
nil
end
end
在条形码.rb中,
def receipt_check?
begin
some code
rescue => e
Rails.logger.error("\nRESCUENIL in receipt_check #{e.class} #{e.message} in #{e.backtrace}")
# FIXME RESCUENIL
nil
end
end
需要干燥代码。如何为我的模型中的所有这些方法编写一个通用的错误记录例程?
最佳答案
您可以为此编写一个抽象,但您无法从那里返回
。你可以写:
def with_log(name)
begin
yield
rescue => exc
Rails.logger.error("\nRESCUENIL in #{name} #{exc.class} #{exc.message} in #{exc.backtrace}")
false
end
end
with_log(:sidekiq_status) do
something
true # not needed if something returns a boolean with the success status
end or return
这个true
也可以移到with_log
,这取决于你打算如何使用它。
关于ruby-on-rails - 救援中的 DRY 代码 => Rails 中各种模型文件中的 e,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27223727/