Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 296
John Bachir Avatar asked Jan 28 '11 15:01

John Bachir


2 Answers

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

like image 114
Peter Brown Avatar answered Oct 23 '22 18:10

Peter Brown


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.

like image 9
Andrew Grimm Avatar answered Oct 23 '22 19:10

Andrew Grimm