Class: SharedTools::Tools::ErrorHandlingTool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/shared_tools/tools/error_handling_tool.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ ErrorHandlingTool

Returns a new instance of ErrorHandlingTool.

Parameters:

  • (defaults to: nil)

    optional logger



93
94
95
96
97
# File 'lib/shared_tools/tools/error_handling_tool.rb', line 93

def initialize(logger: nil)
  @logger = logger || RubyLLM.logger
  @resources_allocated = []
  @operation_start_time = nil
end

Class Method Details

.nameObject



25
# File 'lib/shared_tools/tools/error_handling_tool.rb', line 25

def self.name = 'error_handling_tool'

Instance Method Details

#execute(operation:, simulate_error: nil, max_retries: 3, **data) ⇒ Hash

Execute operation with comprehensive error handling

Parameters:

  • Operation to perform

  • Data to process

  • (defaults to: nil)

    Error type to simulate

  • (defaults to: 3)

    Maximum retry attempts

Returns:

  • Operation result with success status



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/shared_tools/tools/error_handling_tool.rb', line 107

def execute(operation:, simulate_error: nil, max_retries: 3, **data)
  @operation_start_time = Time.now
  @logger.info("ErrorHandlingTool#execute operation=#{operation} simulate_error=#{simulate_error}")

  begin
    # Validate inputs
    validate_preconditions(operation, data, max_retries)

    # Allocate resources (demonstration)
    allocate_resources

    # Perform main operation
    result = perform_operation(operation, data, simulate_error, max_retries)

    # Validate outputs
    validate_postconditions(result)

    @logger.info("Operation completed successfully")

    {
      success:  true,
      result:   result,
      metadata: 
    }
  rescue ValidationError => e
    @logger.error("Validation error: #{e.message}")
    handle_validation_error(e, operation)
  rescue NetworkError => e
    @logger.error("Network error: #{e.message}")
    handle_network_error(e, operation)
  rescue AuthorizationError => e
    @logger.error("Authorization error: #{e.message}")
    handle_authorization_error(e, operation)
  rescue ResourceNotFoundError => e
    @logger.error("Resource not found: #{e.message}")
    handle_resource_not_found_error(e, operation)
  rescue StandardError => e
    @logger.error("General error: #{e.class} - #{e.message}")
    handle_general_error(e, operation)
  ensure
    cleanup_resources
  end
end