Class: OpenAICompatibleErrors::ApiError

Inherits:
Object
  • Object
show all
Defined in:
lib/openai_compatible_errors/error.rb

Overview

A deliberately small immutable snapshot. Raw bodies, headers, causes and tracebacks are not retained, so logging this object is safe by default.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category:, source:, status: nil, code: nil, type: nil, request_id: nil, retry_after_ms: nil, provider_message: nil) ⇒ ApiError

Returns a new instance of ApiError.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/openai_compatible_errors/error.rb', line 52

def initialize(category:, source:, status: nil, code: nil, type: nil,
               request_id: nil, retry_after_ms: nil, provider_message: nil)
  @category = normalize_symbol(category, CATEGORIES, :unknown)
  @source = normalize_symbol(source, SOURCES, :unknown)
  @status = normalize_status(status)
  @code = bounded_text(code, 256)
  @type = bounded_text(type, 256)
  @request_id = bounded_text(request_id, 256)
  @retry_after_ms = normalize_delay(retry_after_ms)
  @provider_message = bounded_text(provider_message, 2_000)
  freeze
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



49
50
51
# File 'lib/openai_compatible_errors/error.rb', line 49

def category
  @category
end

#codeObject (readonly)

Returns the value of attribute code.



49
50
51
# File 'lib/openai_compatible_errors/error.rb', line 49

def code
  @code
end

#provider_messageObject (readonly)

Returns the value of attribute provider_message.



49
50
51
# File 'lib/openai_compatible_errors/error.rb', line 49

def provider_message
  @provider_message
end

#request_idObject (readonly)

Returns the value of attribute request_id.



49
50
51
# File 'lib/openai_compatible_errors/error.rb', line 49

def request_id
  @request_id
end

#retry_after_msObject (readonly)

Returns the value of attribute retry_after_ms.



49
50
51
# File 'lib/openai_compatible_errors/error.rb', line 49

def retry_after_ms
  @retry_after_ms
end

#sourceObject (readonly)

Returns the value of attribute source.



49
50
51
# File 'lib/openai_compatible_errors/error.rb', line 49

def source
  @source
end

#statusObject (readonly)

Returns the value of attribute status.



49
50
51
# File 'lib/openai_compatible_errors/error.rb', line 49

def status
  @status
end

#typeObject (readonly)

Returns the value of attribute type.



49
50
51
# File 'lib/openai_compatible_errors/error.rb', line 49

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



91
92
93
94
95
96
97
# File 'lib/openai_compatible_errors/error.rb', line 91

def ==(other)
  other.is_a?(ApiError) &&
    [@category, @source, @status, @code, @type, @request_id,
     @retry_after_ms, @provider_message] ==
      [other.category, other.source, other.status, other.code, other.type,
       other.request_id, other.retry_after_ms, other.provider_message]
end

#hashObject



101
102
103
104
# File 'lib/openai_compatible_errors/error.rb', line 101

def hash
  [@category, @source, @status, @code, @type, @request_id,
   @retry_after_ms, @provider_message].hash
end

#inspectObject



106
107
108
109
110
# File 'lib/openai_compatible_errors/error.rb', line 106

def inspect
  "#<#{self.class} category=#{@category.inspect} source=#{@source.inspect} " \
    "status=#{@status.inspect} code=#{@code.inspect} type=#{@type.inspect} " \
    "request_id=#{@request_id.inspect} retry_after_ms=#{@retry_after_ms.inspect}>"
end

#messageObject



65
66
67
# File 'lib/openai_compatible_errors/error.rb', line 65

def message
  SAFE_MESSAGES.fetch(@category)
end

#retryable_category?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/openai_compatible_errors/error.rb', line 87

def retryable_category?
  %i[rate_limit timeout network upstream server stream].include?(@category)
end

#to_h(include_provider_message: false) ⇒ Object Also known as: to_log_h



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/openai_compatible_errors/error.rb', line 69

def to_h(include_provider_message: false)
  result = {
    message: message,
    category: @category,
    source: @source
  }
  { status: @status, code: @code, type: @type, request_id: @request_id,
    retry_after_ms: @retry_after_ms }.each do |key, value|
    result[key] = value unless value.nil?
  end
  if include_provider_message && !@provider_message.nil?
    result[:provider_message] = @provider_message
  end
  result.freeze
end

#to_sObject



112
113
114
# File 'lib/openai_compatible_errors/error.rb', line 112

def to_s
  message
end