No commit activity in last 3 years
No release in over 3 years
Saves your from writing `begin...rescue...ensure...end` everywhere. This assumes you know how to use `rescue_from` not just within a controller.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 1.5
>= 0.7
<= 13.0, >= 10.0
~> 3.0
~> 1.0
~> 0.30

Runtime

< 6.0.0, >= 4.1.0
 Project Readme

ExecuteWithRescue

Saves your from writing begin...rescue...ensure...end everywhere.
This assumes you know how to use rescue_from not just within a controller.

I write this "gem" because I enocunter this pattern in many background worker and service classes.
(I use interactor for service classes btw)
I use this gem, rescue_from and a custom airbrake adapter to add options to Airbrake when notifying, since otherwise you will have to call Airbrake.notify_or_ignore manually in rescue and pass the options.
Calling airbrake manually sometimes is the best option, but not all the time.
I might release another gem for that airbrake adapter.

Status

Build Status Gem Version Dependency Status Coverage Status Code Climate

Installation

Add this line to your application's Gemfile:

gem 'execute_with_rescue'

And then execute:

$ bundle

Or install it yourself as:

$ gem install execute_with_rescue

Usage

#execute_with_rescue

Private method to be called with a block
You still have to call rescue_from at class level yourself (no magic here)

class SomeServiceClass
  include ExecuteWithRescue::Mixins::Core

  # then run code with possible errors
  def perform
    execute_with_rescue do
      # Something that might causes error
    end
  end
end

Class Methods for adding hooks

"Hooks" are just things to be run before the block and after the block (in ensure)
Beware that the "hooks" execution order are like ActiveSupport::Callbacks

.add_execute_with_rescue_before_hooks

Alias: .add_execute_with_rescue_before_hook
Execution order: Add first, run first

class SomeServiceClass
  # Either add hooks by using method names in symbol
  add_execute_with_rescue_before_hook :report_start_by_logging

  # Or add more
  add_execute_with_rescue_before_hooks :do_more, :do_even_more

  # Or in block
  add_execute_with_rescue_before_hook do
    Rails.logger.debug("Some job started")
  end

  private

  def report_start_by_logging
    Rails.logger.debug("Some job started")
  end

  def do_more
    # This execute earlier
  end
  def do_even_more
    # This execute later
  end
end

.add_execute_with_rescue_after_hooks

Alias: .add_execute_with_rescue_after_hook
Execution order: Add first, run last

class SomeServiceClass
  # Either add hooks by using method names in symbol
  add_execute_with_rescue_after_hook :report_end_by_logging

  # Or add more
  add_execute_with_rescue_after_hooks :clean_up_base, :clean_up_more

  # Or in block
  add_execute_with_rescue_after_hook do
    Rails.logger.debug("Some job ended")
  end

  private

  def report_end_by_logging
    Rails.logger.debug("Some job ended")
  end

  def clean_up_base
    # This execute later
  end
  def clean_up_more
    # This execute earlier
  end
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b feature/my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin feature/my-new-feature)
  5. Create new Pull Request