Sha256: 07c01dc4969da2d55c9878c3a2e4427e585dcfe876fcbd121b8229c0301b47fc

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module Commander
  ##
  # Internal error class to delay rendering help text
  # This is required as the help command points directly to stdout
  # In general this has a bit of a code smell to it, and should
  # not be used publicly
  class InternalCallableError < StandardError
    attr_accessor :callable

    def initialize(msg = nil, &block)
      super(msg)
      self.callable = block
    end

    def call
      callable.call if callable
    end
  end

  INTERRUPT_MSG = 'Received Interrupt!'

  def self.traceable_error_handler(*args)
    # Determines if there is a --trace flag before a --
    trace_index = args.index do |a|
      if a == '--trace'
        true
      elsif a == '--'
        break
      else
        false
      end
    end

    # Removes the --trace flag if required
    new_args = args.dup
    new_args.delete_at(trace_index) if trace_index

    # Start the actual error handler
    error_handler(!!trace_index) do
      yield(new_args) if block_given?
    end

  rescue Interrupt
    # Start Rescuing Interrupt Immediately
    $stderr.puts INTERRUPT_MSG
    exit 130
  end

  def self.error_handler(trace = false)
    yield if block_given?
  rescue StandardError, Interrupt => e
    $stderr.puts e.full_message if trace

    error_msg = e.message
    exit_code = e.respond_to?(:exit_code) ?  e.exit_code.to_i : 1
    case e
    when InternalCallableError
      # See: https://shapeshed.com/unix-exit-codes/
      exit_code = 126
      $stderr.puts error_msg
      e.call
    when Interrupt
      $stderr.puts INTERRUPT_MSG
      # See: https://shapeshed.com/unix-exit-codes/
      exit_code = 130
    else
      $stderr.puts error_msg
    end
    exit(exit_code)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
commander-openflighthpc-2.1.2 lib/commander/error_handler.rb