Module: ProgramExitFromError

Overview

Idiomatic support for dealing with errors during CLI use

Rather than allowing an error to propagate to the top level of the interpreter, this module provides the #exit_program! method, which prints a message to STDERR and exits with an exit code of 1 (unless otherwise specified with .exit_with_code).

For debugging support, this module also exposes the #exit_code it would use to exit the program were #exit_program! called.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



32
33
34
# File 'lib/program_exit_from_error.rb', line 32

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#exit_codeObject



44
45
46
# File 'lib/program_exit_from_error.rb', line 44

def exit_code
  self.class.instance_variable_get(:@exit_code) || 1
end

#exit_program!Object



48
49
50
51
# File 'lib/program_exit_from_error.rb', line 48

def exit_program!
  print_error_message
  Kernel.exit(exit_code)
end


53
54
55
# File 'lib/program_exit_from_error.rb', line 53

def print_error_message
  $stderr.puts("! ERROR: #{self}".bold.red)
end