Module: Bovem::ShellMethods::General

Included in:
Bovem::Shell
Defined in:
lib/bovem/shell.rb

Overview

General methods.

Instance Method Summary collapse

Instance Method Details

#handle_failure(e, access_error, not_found_error, general_error, entries, fatal, show_errors) ⇒ Object

Handles general failure of a file/directory method.

Parameters:

  • e (Exception)

    The occurred exception.

  • access_error (String|Symbol)

    The message to show in case of access errors.

  • not_found_error (String|Symbol)

    The message to show in case of a not found entry.

  • general_error (String|Symbol)

    The message to show in case of other errors.

  • entries (Array)

    The list of entries which failed.

  • fatal (Boolean)

    If quit in case of fatal errors.

  • show_errors (Boolean)

    Whether to show errors.



21
22
23
24
25
26
27
28
29
# File 'lib/bovem/shell.rb', line 21

def handle_failure(e, access_error, not_found_error, general_error, entries, fatal, show_errors)
  error_type, locale, final_entries = setup_error_handling(entries, fatal)

  case e.class.to_s
    when "Errno::EACCES" then @console.send(error_type, locale.send(access_error, final_entries))
    when "Errno::ENOENT" then @console.send(error_type, locale.send(not_found_error, final_entries))
    else show_general_failure(e, general_error, entries, fatal) if show_errors
  end
end

#setup_error_handling(entries, fatal) ⇒ Array

Setups error handling.

Parameters:

  • entries (Array)

    The list of entries which failed.

  • fatal (Boolean)

    If quit in case of fatal errors.

Returns:

  • (Array)

    Variables for error handling



36
37
38
# File 'lib/bovem/shell.rb', line 36

def setup_error_handling(entries, fatal)
  [fatal ? :fatal : :error, i18n.shell, entries.length == 1 ? entries[0] : entries]
end

#show_general_failure(e, general_error, entries, fatal) ⇒ Object

Shows errors when a directory creation failed.

Parameters:

  • e (Exception)

    The occurred exception.

  • entries (Array)

    The list of entries which failed.

  • fatal (Boolean)

    If quit in case of fatal errors.



45
46
47
48
49
50
51
52
53
54
# File 'lib/bovem/shell.rb', line 45

def show_general_failure(e, general_error, entries, fatal)
  locale = i18n.shell

  @console.error(locale.send(general_error))
  @console.with_indentation(11) do
    entries.each do |entry| @console.write(entry) end
  end
  @console.write(locale.error(e.class.to_s, e), "\n", 5)
  Kernel.exit(-1) if fatal
end