No commit activity in last 3 years
No release in over 3 years
This gem provides a concern that can be included into ruby classes. Once included, you can define exceptions which every class/instance method should swallow and not raise
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

Exception Swallower

Occasionally, you may have a situation where you want all methods in a class to not raise a particular exception. Verbosely this can be accomplished with:

class Foo
  class PopGoesTheWeasel < StandardError; end

  def self.class_method_a
  rescue PopGoesTheWeasel
  end

  def self.class_method_b
  rescue PopGoesTheWeasel
  end

  def instance_method_a
  rescue PopGoesTheWeasel
  end

  def instance_method_b
  rescue PopGoesTheWeasel
  end
end

This differs from something like active_support #rescue_from as it is intended to do nothing with the exceptions and return nil (swallowed).

Installation

Add this line to your application's Gemfile:

gem 'exception_swallower'

And then execute:

$ bundle

Or install it yourself as:

$ gem install exception_swallower

Usage

include the module in any classes you want to be able to swallow exceptions:

class Foo
  include exception_swallower
  ...
end

Once included, you can define an array of exceptions you'd like swallowed for all methods (both class and instance) within the class:

class Foo
  class ExceptionA < StandardError; end
  class ExceptionB < StandardError; end

  include exception_swallower
  swallow_exceptions ExceptionA, ExceptionB
end

When any of these exceptions are raised within a method, they will automatically be rescued and swallowed:

class Foo
  class ExceptionA < StandardError; end
  class ExceptionB < StandardError; end

  include exception_swallower
  swallow_exceptions ExceptionA, ExceptionB

  def self.my_method
    puts "it's working!"
    raise ExceptionA
  end
end

Foo.my_method #=> "it's working!"