How to rescue exception central and DRY?

…衆ロ難τιáo~ 提交于 2020-01-23 01:02:09

问题


I have an exception that is produced at ~20 seperate places. It can be rescued easy and in the same way at every place but thats not dry and eaven a crapy work! I want to rescue this exception at a central position. How can I arrange this?

Its about the ActiveRecord::RecordNonUnique exception by the way,...


回答1:


What about this ?

def rescue_from_record_non_unique
  yield
rescue ActiveRecord::RecordNonUnique
  # your code
end

# ...

rescue_from_record_non_unique do
  # do something
end


来源:https://stackoverflow.com/questions/8644577/how-to-rescue-exception-central-and-dry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!