
Security News
minimatch Patches 3 High-Severity ReDoS Vulnerabilities
minimatch patched three high-severity ReDoS vulnerabilities that can stall the Node.js event loop, and Socket has released free certified patches.
rescue_from
Advanced tools
An imitation of ActiveSupport::Rescuable that relies on Ruby's callbacks to reduce boilerplate code.
Add this line to your application's Gemfile:
gem 'rescue_from', '~> 2.0'
And then execute:
$ bundle
Or you can install the gem on its own:
gem install rescue_from
Simply extend with RescueFrom::Rescuable any class or module that you want to use rescue_from in.
class DataImporter
extend RescueFrom::Rescuable
rescue_from JSON::ParserError do
[]
end
def temperature_data
JSON.parse File.read('temperatures.json')
end
def humidity_data
JSON.parse File.read('humidity.json')
end
end
Both #temperature_data and #humidity_data will return an empty array if the respective files contain JSON syntax errors.
rescue_from can accept multiple patterns, and will associate the same handler with all of them. Handlers are looked up top to bottom, left to right, and up the ancestors chain as it is returned by self.class.ancestors. The lookup uses case equality to match patterns against the raised exception, so you can use less straightforward patterns instead of classes. This class will silence all exceptions the messages of which don't start with "failure":
class Service
extend RescueFrom::Rescuable
rescue_from(proc {|e| !e.message.start_with? 'failure'}) do
nil
end
...
end
If no pattern matches the exception, it will be reraised.
Handlers are called in the context of the receiver of the method that raised the exception.
If you want to call a given method bypassing the automatic exception handling, you can append _without_rescue to its name:
di = DataImporter.new
di.temperature_data # => []
di.temperature_data_without_rescue # => JSON::ParserError
Sometimes, instead of silencing the exception, you want to change its class, for example if you're using the same low level library in different parts of the application and you need to distinguish the exceptions coming from the different sides. In such cases, you can use relabel:
class Service
extend RescueFrom::Rescuable
relabel pattern1, pattern2, ..., to: ExceptionClass do |e|
code
end
...
end
is equivalent to
class Service
extend RescueFrom::Rescuable
rescue_from pattern1, pattern2, ... do |e|
message = code
raise ExceptionClass, message
end
...
end
If you want to limit automatic exception handling to only certain methods, you can override should_rescue_in?. It will be called every time a new method is defined, receiving its name as single argument; if it returns false or nil, the method is left untouched.
class Service
extend RescueFrom::Rescuable
ACTIONS = [:create, :update, :delete]
def self.should_rescue_in? method_name
ACTIONS.include? method_name
end
...
end
Rescue From loosely follows Semantic Versioning, with a hard guarantee that breaking changes to the public API will always coincide with an increase to the MAJOR number.
Version numbers are in three parts: MAJOR.MINOR.PATCH.
MAJOR. There may also be changes that would otherwise increase the MINOR or the PATCH.MINOR. There may also be changes that would otherwise increase the PATCH.PATCH.Notice that any feature deprecated by a minor release can be expected to be removed by the next major release.
Full list of changes in CHANGELOG.md
Bug reports and pull requests are welcome on GitHub at https://github.com/moku-io/rescue_from.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that rescue_from demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
minimatch patched three high-severity ReDoS vulnerabilities that can stall the Node.js event loop, and Socket has released free certified patches.

Research
/Security News
Socket uncovered 26 malicious npm packages tied to North Korea's Contagious Interview campaign, retrieving a live 9-module infostealer and RAT from the adversary's C2.

Research
An impersonated golang.org/x/crypto clone exfiltrates passwords, executes a remote shell stager, and delivers a Rekoobe backdoor on Linux.