如何拯救异常中央和干? [英] How to rescue exception central and DRY?

查看:97
本文介绍了如何拯救异常中央和干?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是产生于20〜单独的地方例外。它可以被救出容易,以同样的方式,在每一个地方,但是那不是干,eaven一个crapy的工作!我想在一个中心位置,以拯救这一例外。我该如何安排呢?

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?

其关于的ActiveRecord :: RecordNonUnique 异常的方式,...

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

推荐答案

这个怎么样?

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

# ...

rescue_from_record_non_unique do
  # do something
end

这篇关于如何拯救异常中央和干?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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