Skip to content

Instantly share code, notes, and snippets.

@leompeters
Last active May 7, 2020 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leompeters/d7aae0b6c1426f0c1c2d1a68f18efe3e to your computer and use it in GitHub Desktop.
Save leompeters/d7aae0b6c1426f0c1c2d1a68f18efe3e to your computer and use it in GitHub Desktop.
Ruby custom error classes: inheritance of the message attribute.
# 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