什么是在rails项目中保留自定义异常定义的传统位置?

Joh*_*hir 12 ruby ruby-on-rails exception

制作一个自己的自定义异常时

class ThingExploded < StandardError; end
class ThingIsMissing < StandardError; end
Run Code Online (Sandbox Code Playgroud)

保存这些的好地方在哪里?我正在考虑lib/exceptions.rb ...并且还在考虑是否更适合以某种方式使它们更接近使用它们的代码.

Pet*_*own 17

我可能会使用lib/exceptions/thing_exploded.rb将每个类保存在一个单独的文件中.


And*_*imm 9

除非您的例外情况如此严重,否则不应将其从中拯救出来,将其分类Exception是不合适的.

例外,如fatalNoMemoryError有例外的子类,所以如果你有这样的代码rescue Exception来处理ThingExplodedThingIsMissing,你会抢救各种东西,最好单独留在家中.

最好从它们中继承它们StandardError.


Joh*_*hir 1

我将继续app/models/model_name/exceptions.rb,将它们保留在模块内。