Last active
May 11, 2016 23:59
-
-
Save rohanthewiz/66a5eb50117ab975ba4f9844432f00b1 to your computer and use it in GitHub Desktop.
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
# A Pattern for Custom Exception types | |
module Orders | |
class OrdersError < StandardError; end | |
class OrdersLineItemError < OrdersError | |
def message | |
'There was a line item error.' | |
end | |
end | |
class OrdersOtherError < OrdersError | |
def message | |
'There was another kind of error' | |
end | |
end | |
class OrdersLineItem | |
OKAY = true | |
def apply | |
status = false | |
#... | |
fail OrdersLineItemError, 'Could put message here too' unless status == OKAY | |
rescue OrdersError => e | |
puts "#{e} - #{e.message}" | |
end | |
end | |
end | |
order = Orders::OrdersLineItem.new | |
order.apply # => Could put message here too - There was a line item error. | |
# Thanks to Panthomakos for the basic pattern: https://gist.github.com/panthomakos/1230673 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment