Last active
May 7, 2020 15:43
-
-
Save leompeters/d7aae0b6c1426f0c1c2d1a68f18efe3e to your computer and use it in GitHub Desktop.
Ruby custom error classes: inheritance of the message attribute.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby custom error classes: inheritance of the message attribute. | |
# | |
# Create a custom exception class in Ruby. | |
# | |
# ===== Example: | |
# | |
# begin | |
# raise MyCustomError.new(my_object), "a message" | |
# rescue MyCustomError => e | |
# puts e.message # => "a message" | |
# puts e.object.inspect # => my_object: {...} | |
# end | |
# | |
# See:: https://stackoverflow.com/a/16108216/2334082 | |
# | |
class MyCustomError < StandardError | |
attr_reader :object | |
def initialize(object = nil) | |
super I18n.t('error...') | |
@object = object | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment