Class: SkillBench::Clients::ResponseErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/skill_bench/clients/response_error_handler.rb

Overview

Handles error responses and logging for LLM provider clients. Encapsulates error formatting, logging, and exception handling.

Constant Summary collapse

API_FAILED =
'API Request failed'

Class Method Summary collapse

Class Method Details

.failure_response(response, parsed, &usage_extractor) ⇒ Hash

Creates an error response for failed HTTP requests.

Parameters:

  • response (Faraday::Response)

    The HTTP response

  • parsed (Hash)

    Parsed response body

  • usage_extractor (Proc)

    Block to extract usage data

Returns:

  • (Hash)

    Standardized error response



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/skill_bench/clients/response_error_handler.rb', line 16

def self.failure_response(response, parsed, &usage_extractor)
  error_msg = "#{API_FAILED}: #{response.status}"
  detail = parsed.is_a?(Hash) ? (parsed[:error] || parsed['error'] || parsed) : parsed

  if detail.is_a?(Hash) && (detail[:message] || detail['message'])
    error_msg += " - #{detail[:message] || detail['message']}"
  elsif !detail.to_s.empty?
    error_msg += " - #{detail}"
  end

  {
    success: false,
    result: error_msg,
    usage: usage_extractor.call(parsed),
    response: { error: { message: error_msg } },
    status: 'error',
    code: response.status
  }
end

.handle_exception(error, type) ⇒ Hash

Handles an exception by logging and returning a standardized error response.

Parameters:

  • error (StandardError)

    The exception that occurred

  • type (String)

    The error type label

Returns:

  • (Hash)

    Standardized error response



59
60
61
62
# File 'lib/skill_bench/clients/response_error_handler.rb', line 59

def self.handle_exception(error, type)
  log_error(error)
  { success: false, result: "#{type}: #{error.message}", status: 'error' }
end

.log_error(error) ⇒ void

This method returns an undefined value.

Logs an error message and backtrace to Rails.logger or stderr.

Parameters:

  • error (StandardError)

    The exception to log



68
69
70
# File 'lib/skill_bench/clients/response_error_handler.rb', line 68

def self.log_error(error)
  SkillBench::ErrorLogger.log_error(error)
end

.missing_message_response(response, parsed, &usage_extractor) ⇒ Hash

Creates an error response when the LLM response has no message content.

Parameters:

  • response (Faraday::Response)

    The HTTP response

  • parsed (Hash)

    Parsed response body

  • usage_extractor (Proc)

    Block to extract usage data

Returns:

  • (Hash)

    Standardized error response



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/skill_bench/clients/response_error_handler.rb', line 42

def self.missing_message_response(response, parsed, &usage_extractor)
  error_msg = 'LLM response missing message content'
  {
    success: false,
    result: error_msg,
    usage: usage_extractor.call(parsed),
    response: { error: { message: error_msg } },
    status: 'error',
    code: response.status
  }
end