Class: CLI::Kit::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/kit/error_handler.rb

Defined Under Namespace

Classes: ExceptionReporter, NullExceptionReporter

Constant Summary collapse

SIGNALS_THAT_ARENT_BUGS =
[
  'SIGTERM', 'SIGHUP', 'SIGINT',
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_file: nil, exception_reporter: NullExceptionReporter, tool_name: nil, dev_mode: false) ⇒ ErrorHandler

: (?log_file: String?, ?exception_reporter: exception_reporter_or_proc, ?tool_name: String?, ?dev_mode: bool) -> void



15
16
17
18
19
20
# File 'lib/cli/kit/error_handler.rb', line 15

def initialize(log_file: nil, exception_reporter: NullExceptionReporter, tool_name: nil, dev_mode: false)
  @log_file = log_file
  @exception_reporter_or_proc = exception_reporter
  @tool_name = tool_name
  @dev_mode = dev_mode
end

Instance Attribute Details

#override_exception_handler=(value) ⇒ Object (writeonly)

: ^(Exception arg0) -> Integer



12
13
14
# File 'lib/cli/kit/error_handler.rb', line 12

def override_exception_handler=(value)
  @override_exception_handler = value
end

Instance Method Details

#call(&block) ⇒ Object

: { -> void } -> Integer



44
45
46
47
48
49
# File 'lib/cli/kit/error_handler.rb', line 44

def call(&block)
  # @at_exit_exception is set if handle_abort decides to submit an error.
  # $ERROR_INFO is set if we terminate because of a signal.
  at_exit { report_exception(@at_exit_exception || $ERROR_INFO) }
  triage_all_exceptions(&block)
end

#report_exception(error) ⇒ Object

: (Exception? error) -> void



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cli/kit/error_handler.rb', line 52

def report_exception(error)
  if (notify_with = exception_for_submission(error))
    logs = nil
    if @log_file
      logs = begin
        File.read(@log_file)
      rescue => e
        "(#{e.class}: #{e.message})"
      end
    end
    exception_reporter.report(notify_with, logs)
  end
end