Class: RosettAi::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rosett_ai/error_handler.rb

Overview

Centralized error handler for CLI commands.

Catches +RosettAi::Error+ (and subclasses), formats the error message with what/why/fix structure, and exits with the mapped exit code. Stack traces are only shown when +RAI_LOG_LEVEL=DEBUG+.

TTY-aware: uses colour and formatting when stdout is a TTY, plain text when piped.

See Also:

  • ExitCodes
  • conf/design/error_handlingconf/design/error_handling.yml

Author:

  • hugo

  • claude

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stderr: $stderr) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.

Parameters:

  • stderr (IO) (defaults to: $stderr)

    output stream for errors



31
32
33
# File 'lib/rosett_ai/error_handler.rb', line 31

def initialize(stderr: $stderr)
  @stderr = stderr
end

Class Method Details

.handle(stderr: $stderr) { ... } ⇒ Integer

Wraps a block, rescuing +RosettAi::Error+ and formatting the output.

Parameters:

  • stderr (IO) (defaults to: $stderr)

    output stream for errors (default: $stderr)

Yields:

  • the CLI command block to execute

Returns:

  • (Integer)

    exit code (0 on success)



26
27
28
# File 'lib/rosett_ai/error_handler.rb', line 26

def self.handle(stderr: $stderr, &)
  new(stderr: stderr).handle(&)
end

Instance Method Details

#handle { ... } ⇒ Integer

Executes the block and handles errors.

Yields:

  • the CLI command block

Returns:

  • (Integer)

    exit code



39
40
41
42
43
44
45
# File 'lib/rosett_ai/error_handler.rb', line 39

def handle
  yield
  ExitCodes::SUCCESS
rescue RosettAi::Error => e
  report(e)
  ExitCodes.for(e)
end