ExceptionSuppressor.suppress()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
dl 0
loc 11
ccs 4
cts 4
cp 1
crap 3
rs 9.85
1 1
module Resume
2
  # Module encapsulating all the functionality of the command line
3
  # interface to generate the resume.
4
  #
5
  # @author Paul Fioravanti
6 1
  module CLI
7
    # Module concerned with suppressing exceptions of a specific type.
8
    #
9
    # @author Paul Fioravanti
10 1
    module ExceptionSuppressor
11 1
      module_function
12
13
      # Suppresses exception of type `error_to_ignore` and `call`s
14
      # code contained inside `default` if exception is to be ignored.
15
      #
16
      # @param error_to_ignore [Exception]
17
      #   The exception type to ignore.
18
      # @param default [Proc]
19
      #   The code to be executed when the exception is to be ignored.
20
      # @raise [Exception]
21
      #   The exception to be raised if it is not to be ignored.
22
      # @return the result of the passed-in lambda
23 1
      def suppress(error_to_ignore = StandardError, default = -> {})
24 16
        yield
25
      # NOTE: This method needs to rescue from LoadError and Gem::LoadError
26
      # which don't inherit from StandardError, hence needing to rescue from
27
      # the Exception class.
28
      # rubocop:disable Lint/RescueException
29
      rescue Exception => error
30 8
        raise error unless error.is_a?(error_to_ignore)
31
32 5
        default.call
33
      end
34
      # rubocop:enable Lint/RescueException
35
    end
36
  end
37
end
38