Module: Rake

Defined in:
lib/rake-protect.rb

Overview

rubocop:disable Style/Documentation

Class Method Summary collapse

Class Method Details

.protect(&block) ⇒ Object

Wraps block in rescue clause to prevent raising an exception when some gems are unavailable in an environment, for example on a production server.

Examples:

Rake.protect do
  require 'unavailable_gem'
  ..
end


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rake-protect.rb', line 16

def self.protect(&block)
  raise ArgumentError unless block

  block.call
rescue LoadError => error
  return unless Rake.verbose == true

  if RUBY_VERSION >= '2.1'
    # :nocov:
    task = error.backtrace_locations.find do |location|
      location.path.match(/^.*\.rake$/)
    end
    # :nocov:
  end

  $stderr.puts Rainbow("Tasks#{(' in ' + task.path) if task} not available: #{error.message}").red
end