Last active
August 29, 2015 14:01
-
-
Save sharipov-ru/f96f35fb3a8b7582491d to your computer and use it in GitHub Desktop.
Exceptions handling with ruby 2.1
This file contains 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
class ServiceException < StandardError; end | |
class RepositoryException < StandardError; end | |
class RethinkDBError < StandardError; end | |
class RecordsController | |
def self.update | |
RecordsService.update(@record) | |
rescue ServiceException => error | |
puts "Failure: #{error.inspect}" | |
puts "Original failure: #{error.cause.inspect}" | |
puts "Really Original failure: #{error.cause.cause.inspect}" | |
# exception logging and notifications (with original causes) | |
end | |
end | |
class RecordsService | |
def self.update(record) | |
RecordsRepository.update(record) | |
rescue RepositoryException => error | |
raise ServiceException, "Records Service exception" | |
end | |
end | |
class RecordsRepository | |
def self.update(record) | |
raise RepositoryException, "argument some_id cannot be nil" if false | |
raise RepositoryException, "argument some_othe_id cannot be nil" if false | |
Mapper.update(record) | |
rescue => error | |
raise RepositoryException, "General database exception" | |
end | |
end | |
class Mapper | |
def self.update(record) | |
RethinkDB.update({id: record.id, name: record.name}) | |
end | |
end | |
class RethinkDB | |
def self.update(options) | |
raise RethinkDBError, "RethinkDB: unable to find the field blah-blah-blah" | |
end | |
end | |
# Request: | |
RecordsController.update | |
# Output: | |
Failure: #<ServiceException: Records Service exception> | |
Original failure: #<RepositoryException: General database exception> | |
Really Original failure: #<RethinkDBError: RethinkDB: unable to find the field blah-blah-blah> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment