Skip to content

Instantly share code, notes, and snippets.

@sharipov-ru
Last active August 29, 2015 14:01
Show Gist options
  • Save sharipov-ru/f96f35fb3a8b7582491d to your computer and use it in GitHub Desktop.
Save sharipov-ru/f96f35fb3a8b7582491d to your computer and use it in GitHub Desktop.
Exceptions handling with ruby 2.1
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