Exception: Ace::Support::Cli::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/ace/support/cli/error.rb

Overview

Exception raised to signal non-zero exit code from CLI commands.

This exception is used in the exception-based exit code pattern defined in ADR-023. Commands raise this error on failure, and the exe wrapper catches it and exits with the specified code.

Examples:

Raising from a command

def call(file:, **options)
  raise Error.new("file required") if file.nil?

  result = do_work(file)

  if result[:success]
    puts result[:message]
    # Success - no exception, exits 0
  else
    raise Error.new(result[:error])
  end
end

Catching in exe wrapper

# exe/ace-gem
begin
  Ace::Gem::CLI.start(ARGV)
rescue Ace::Support::Cli::Error => e
  warn e.message
  exit(e.exit_code)
end

See Also:

  • CLI framework conventions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, exit_code: 1) ⇒ Error

Initialize a new CLI error

Parameters:

  • message (String)

    Error message to display

  • exit_code (Integer) (defaults to: 1)

    Exit code (default: 1)



49
50
51
52
53
# File 'lib/ace/support/cli/error.rb', line 49

def initialize(message, exit_code: 1)
  @original_message = message
  super(message)
  @exit_code = exit_code
end

Instance Attribute Details

#exit_codeInteger (readonly)

Exit code to return when this exception is caught

Returns:

  • (Integer)


39
40
41
# File 'lib/ace/support/cli/error.rb', line 39

def exit_code
  @exit_code
end

#original_messageString (readonly)

Original error message without prefix

Returns:

  • (String)


43
44
45
# File 'lib/ace/support/cli/error.rb', line 43

def original_message
  @original_message
end

Instance Method Details

#messageString

Return the original message without prefix. This ensures .message returns what was passed to the constructor.

Returns:

  • (String)

    Original message



58
59
60
# File 'lib/ace/support/cli/error.rb', line 58

def message
  @original_message
end

#to_sString

Prepend “Error: ” to message for consistent user-facing output. exe wrappers use warn e.to_s which calls this method.

Returns:

  • (String)

    Message with “Error: ” prefix



65
66
67
# File 'lib/ace/support/cli/error.rb', line 65

def to_s
  "Error: #{@original_message}"
end