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 "\n  The following external command appears to have failed (exit status \#{error.exit_code}):\n    \#{error.command}\n\n  The output of the command has been logged to \#{log_file}\n      ERROR\n    when \"Hem::InvalidCommandOrOpt\"\n      Hem.ui.error \"\\n\#{error.message}\"\n      Hem.ui.info cli.help_formatter.help\n    when \"Hem::MissingArgumentsError\"\n      Hem.ui.error \"\\n\#{error.message}\"\n      Hem.ui.info cli.help_formatter.help(target: error.command)\n    when \"Hem::UserError\"\n      Hem.ui.error \"\\n\#{error.message}\\n\"\n    when \"Hem::ProjectOnlyError\"\n      Hem.ui.error \"\\nHem requires you to be in a project directory for this command!\\n\"\n    when \"Hem::HostCheckError\"\n      Hem.ui.error \"\\nHem has detected a problem with your system configuration:\\n\"\n      Hem.ui.warning error.advice.gsub(/^/, '  ')\n    when \"Hem::Error\"\n      Hem.ui.error \"\\n\#{error.message}\\n\"\n    else\n      File.write(log_file, \"(\#{error.class}) \#{error.message}\\n\\n\#{error.backtrace.join(\"\\n\")}\")\n      Hem.ui.error <<-ERROR\n\n  An unexpected error has occured:\n    \#{error.message}\n\n  The backtrace has been logged to \#{log_file}\n      ERROR\n  end\n\n  return EXIT_CODES[error.class.to_s] || DEFAULT_EXIT_CODE\nend\n"