什么是在rails项目中保留自定义exception定义的传统位置?
制作一个自己的自定义exception时
class ThingExploded < StandardError; end class ThingIsMissing < StandardError; end
保存这些的好地方在哪里? 我正在考虑lib / exceptions.rb …并且还在考虑是否更适合以某种方式使它们更接近使用它们的代码。
我可能会使用lib / exceptions / thing_exploded.rb将每个类保存在一个单独的文件中。
除非您的例外情况如此严重,否则不应该从中获救,从Exception
inheritance它们是不合适的。
例如fatal
和NoMemoryError
类的exception是Exception的子类,所以如果你有一些诸如rescue Exception
类的代码来处理ThingExploded
和ThingIsMissing
,你就会拯救各种最好的东西。
最好从StandardError
inheritance它们。
我要使用app/models/model_name/exceptions.rb
,将它们保留在模块中。