class ThreeScaleToolbox::CLI::ErrorHandler

Public Class Methods

error_watchdog() { || ... } click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 4
def self.error_watchdog
  new.error_watchdog { yield }
end

Public Instance Methods

error_watchdog() { || ... } click to toggle source

Catches errors and prints nice diagnostic messages

# File lib/3scale_toolbox/cli/error_handler.rb, line 9
def error_watchdog
  # Run
  yield
rescue StandardError, ScriptError => e
  handle_error e
  e
else
  nil
end

Private Instance Methods

error_serialize(error) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 107
def error_serialize(error)
  JSON.pretty_generate format_error(error)
end
expected_error?(error) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 29
def expected_error?(error)
  case error
  when ThreeScaleToolbox::Error
    true
  else
    false
  end
end
format_error(error) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 111
def format_error(error)
  {
    code: error.code,
    message: error.message,
    class: error.kind,
    stacktrace: error.stacktrace
  }.compact
end
gems_and_versions() click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 91
def gems_and_versions
  gems = {}
  Gem::Specification.find_all.sort_by { |s| [s.name, s.version] }.each do |spec|
    gems[spec.name] ||= []
    gems[spec.name] << spec.version.to_s
  end
  gems
end
handle_error(error) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 21
def handle_error(error)
  if expected_error?(error)
    warn error_serialize(error)
  else
    print_unexpected_error(error)
  end
end
print_unexpected_error(error) click to toggle source
write_error_message(error, stream) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 46
def write_error_message(error, stream)
  write_section_header(stream, 'Message')
  stream.puts "\e[1m\e[31m#{error.class}: #{error.message}\e[0m"
end
write_installed_gems(stream) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 66
def write_installed_gems(stream)
  write_section_header(stream, 'Installed gems')
  gems_and_versions.each do |g|
    stream.puts "  #{g.first} #{g.last.join(', ')}"
  end
end
write_load_paths(stream) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 73
def write_load_paths(stream)
  write_section_header(stream, 'Load paths')
  $LOAD_PATH.each_with_index do |i, index|
    stream.puts "  #{index}. #{i}"
  end
end
write_section_header(stream, title) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 100
def write_section_header(stream, title)
  stream.puts

  stream.puts "===== #{title.upcase}:"
  stream.puts
end
write_stack_trace(error, stream) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 51
def write_stack_trace(error, stream)
  write_section_header(stream, 'Backtrace')
  stream.puts error.backtrace
end
write_system_information(stream) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 61
def write_system_information(stream)
  write_section_header(stream, 'System Information')
  stream.puts Etc.uname.to_json
end
write_verbose_error(error, stream) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 80
def write_verbose_error(error, stream)
  stream.puts "Crashlog created at #{Time.now}"

  write_error_message(error, stream)
  write_stack_trace(error, stream)
  write_version_information(stream)
  write_system_information(stream)
  write_installed_gems(stream)
  write_load_paths(stream)
end
write_version_information(stream) click to toggle source
# File lib/3scale_toolbox/cli/error_handler.rb, line 56
def write_version_information(stream)
  write_section_header(stream, 'Version Information')
  stream.puts ThreeScaleToolbox::VERSION
end