开发者

What is a conventional place to keep custom Exception definitions in a rails project? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 9 years ago.

Improve this开发者_开发知识库 question

When making one's own custom exceptions like

class ThingExploded < StandardError; end
class ThingIsMissing < StandardError; end

Where is a good place to keep these? I was considering lib/exceptions.rb… and also pondering if it would be more appropriate to somehow put them closer to the code that uses them.


I would probably go with lib/exceptions/thing_exploded.rb to keep each class in a separate file.


Unless your exceptions are so severe they shouldn't be rescued from, subclassing them from Exception isn't appropriate.

Exceptions such as fatal and NoMemoryError are subclasses of Exception, so if you had code such as rescue Exception to handle ThingExploded and ThingIsMissing, you'd be rescuing all kinds of stuff that are best left alone.

It's better to subclass them from StandardError instead.


I'm going with app/models/model_name/exceptions.rb, keeping them inside the module.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜