Getting a list of existing Rails Error classes for re-use/inheritance

Viewed 10454

Often I need to throw a custom(ized) error. Like when a resource cannot be found due to a mismatch in parameters or so.

I prefer to throw existing errors or, throw an error that is inherited from an existing error. That way, I don't introduce Error classes that were already defined and could have been used perfectly (DRY). But it also allows to keep wording and style the same, by inheriting and simply changing a word or two to clarify the difference with the original Error.

For example:

Foo.new
Foo.some_external_id = nil
Foo.fetch_external_resource
# => InvalidOptions: Calling Foo#fetch_external_resource with nil is invalid

I am quite sure such errors are already defined. In fact, after reading trough many lines of code, I found my MongoID driver has Mongoid::Errors::InvalidOptions: Calling Document#find with nil is invalid.

Is there a list of available Error classes in Ruby Core and Ruby on Rails? Is there a way to get such a list for your current project?

Is it smart at all to re-use and/or inherit existing errors, or should I maintain my own, custom set instead?

3 Answers
Related