Skip to content

Instantly share code, notes, and snippets.

@rohanthewiz
Last active May 11, 2016 23:59
Show Gist options
  • Save rohanthewiz/66a5eb50117ab975ba4f9844432f00b1 to your computer and use it in GitHub Desktop.
Save rohanthewiz/66a5eb50117ab975ba4f9844432f00b1 to your computer and use it in GitHub Desktop.
# 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