Deferred Exception
Have you ever found yourself writing code like this? Have you presumed that you would notice a problem and go looking in your logs to find the problem?
class EmailBlaster
def self.process_all!
all.each { |email| email.blast! }
end
def blast!
begin
# do some atomic work here
rescue StandardError => e
Logger.debug("Something went wrong #{e.}")
end
end
# &c.
end
Wouldn't it be nice, nay preferable, to accrue exceptions without stopping the subsequent atomic operations?
Installation
Add this line to your application's Gemfile:
gem 'deferred_exception'
And then execute:
$ bundle
Or install it yourself as:
$ gem install deferred_exception
Usage
Deferred Exception adds a defer_exceptions
method to the Enumerable
module
that wraps the enumberable object and stores any exceptions that are raised
during iteration.
class EmailBlaster
def self.process_all!
all.defer_exceptions.each do { |email| email.blast! }
end
def blast!
# important, atomic work here
end
end
Any exceptions will be caught and queued into a ExceptionSet
class, a
subclass of StandardError
and will be raised once the iteration is complete.
ExceptionSet#message
will be a string containing messages and backtraces from
all caught exceptions
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request