Class: CLI::Kit::ErrorHandler

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/cli/kit/error_handler.rb

Defined Under Namespace

Classes: ExceptionReporter, NullExceptionReporter

Constant Summary collapse

ExceptionReporterOrProc =
T.type_alias do
  T.any(T.class_of(ExceptionReporter), T.proc.returns(T.class_of(ExceptionReporter)))
end
SIGNALS_THAT_ARENT_BUGS =
[
  'SIGTERM', 'SIGHUP', 'SIGINT',
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from T::Sig

sig

Constructor Details

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

Returns a new instance of ErrorHandler.



26
27
28
29
30
31
# File 'lib/cli/kit/error_handler.rb', line 26

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)

Sets the attribute override_exception_handler

Parameters:

  • value

    the value to set the attribute override_exception_handler to.



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

def override_exception_handler=(value)
  @override_exception_handler = value
end

Instance Method Details

#call(&block) ⇒ Object



60
61
62
63
64
65
# File 'lib/cli/kit/error_handler.rb', line 60

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



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cli/kit/error_handler.rb', line 68

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