Class: Hem::ErrorHandlers::Friendly

Inherits:
Object
  • Object
show all
Includes:
ExitCodeMap
Defined in:
lib/hem/error_handlers/friendly.rb

Constant Summary

Constants included from ExitCodeMap

ExitCodeMap::DEFAULT_EXIT_CODE, ExitCodeMap::EXIT_CODES

Instance Method Summary collapse

Instance Method Details

#handle(cli, error) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hem/error_handlers/friendly.rb', line 6

def handle cli, error
  require 'tmpdir'
  log_file = File.join(Dir.tmpdir, 'hem_error.log')

  # Not possible to match Interrupt class unless we use class name as string for some reason!
  case error.class.to_s
    when "Interrupt"
      Hem.ui.warning "\n\nCaught Interrupt. Aborting\n"
    when "Hem::ExternalCommandError"
      FileUtils.cp error.output.path, log_file

      File.open(log_file, "a") do |file|
        file.write "\n(#{error.class}) #{error.message}\n\n#{error.backtrace.join("\n")}"
      end

      Hem.ui.error <<-ERROR

  The following external command appears to have failed (exit status #{error.exit_code}):
    #{error.command}

  The output of the command has been logged to #{log_file}
      ERROR
    when "Hem::InvalidCommandOrOpt"
      Hem.ui.error "\n#{error.message}"
      Hem.ui.info cli.help_formatter.help
    when "Hem::MissingArgumentsError"
      Hem.ui.error "\n#{error.message}"
      Hem.ui.info cli.help_formatter.help(target: error.command)
    when "Hem::UserError"
      Hem.ui.error "\n#{error.message}\n"
    when "Hem::ProjectOnlyError"
      Hem.ui.error "\nHem requires you to be in a project directory for this command!\n"
    when "Hem::HostCheckError"
      Hem.ui.error "\nHem has detected a problem with your system configuration:\n"
      Hem.ui.warning error.advice.gsub(/^/, '  ')
    when "Hem::Error"
      Hem.ui.error "\n#{error.message}\n"
    else
      File.write(log_file, "(#{error.class}) #{error.message}\n\n#{error.backtrace.join("\n")}")
      Hem.ui.error <<-ERROR

  An unexpected error has occured:
    #{error.message}

  The backtrace has been logged to #{log_file}
      ERROR
  end

  return EXIT_CODES[error.class.to_s] || DEFAULT_EXIT_CODE
end