lib/cli/kit/error_handler.rb in cli-kit-3.0.0.pre vs lib/cli/kit/error_handler.rb in cli-kit-3.0.0

- old
+ new

@@ -1,42 +1,49 @@ require 'cli/kit' +require 'English' module CLI module Kit class ErrorHandler - def initialize(log_file: nil, exception_reporter: NullExceptionReporter) + def initialize(log_file:, exception_reporter:) @log_file = log_file - @exception_reporter_or_proc = exception_reporter + @exception_reporter_or_proc = exception_reporter || NullExceptionReporter end module NullExceptionReporter - def self.report(exception, logs) + def self.report(_exception, _logs) nil end end + def call(&block) + install! + handle_abort(&block) + end + + private + def install! at_exit { handle_final_exception(@exception || $ERROR_INFO) } end def handle_abort yield + CLI::Kit::EXIT_SUCCESS rescue CLI::Kit::GenericAbort => e is_bug = e.is_a?(CLI::Kit::Bug) || e.is_a?(CLI::Kit::BugSilent) is_silent = e.is_a?(CLI::Kit::AbortSilent) || e.is_a?(CLI::Kit::BugSilent) print_error_message(e) unless is_silent (@exception = e) if is_bug CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG rescue Interrupt - STDERR.puts(format_error_message("Interrupt")) + $stderr.puts(format_error_message("Interrupt")) return CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG end - private - def handle_final_exception(error) notify_with = nil case error when nil # normal, non-error termination @@ -45,17 +52,17 @@ when SignalException skip = %w(SIGTERM SIGHUP SIGINT) unless skip.include?(error.message) notify_with = error end - when SystemExit # "exit N" called + when SystemExit # "exit N" called case error.status - when CLI::Kit::EXIT_SUCCESS # submit nothing if it was `exit 0` + when CLI::Kit::EXIT_SUCCESS # submit nothing if it was `exit 0` when CLI::Kit::EXIT_FAILURE_BUT_NOT_BUG - # if it was `exit 30`, translate the exit code to 1, and submit nothing + # if it was `exit 30`, translate the exit code to 1, and submit nothing. # 30 is used to signal normal failures that are not indicative of bugs. - # But users should see it presented as 1. + # However, users should see it presented as 1. exit 1 else # A weird termination status happened. `error.exception "message"` will maintain backtrace # but allow us to set a message notify_with = error.exception "abnormal termination status: #{error.status}" @@ -68,11 +75,11 @@ logs = begin File.read(@log_file) rescue => e "(#{e.class}: #{e.message})" end - exceptiono_reporter.report(notify_with, logs) + exception_reporter.report(notify_with, logs) end end def exception_reporter if @exception_reporter_or_proc.respond_to?(:report) @@ -84,12 +91,11 @@ def format_error_message(msg) CLI::UI.fmt("{{red:#{msg}}}") end - def print_error_message(e) - STDERR.puts(format_error_message(e.message)) + $stderr.puts(format_error_message(e.message)) end end end end