Class: OpenAICompatibleErrors::ApiError
- Inherits:
-
Object
- Object
- OpenAICompatibleErrors::ApiError
- 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
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#provider_message ⇒ Object
readonly
Returns the value of attribute provider_message.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#retry_after_ms ⇒ Object
readonly
Returns the value of attribute retry_after_ms.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(category:, source:, status: nil, code: nil, type: nil, request_id: nil, retry_after_ms: nil, provider_message: nil) ⇒ ApiError
constructor
A new instance of ApiError.
- #inspect ⇒ Object
- #message ⇒ Object
- #retryable_category? ⇒ Boolean
- #to_h(include_provider_message: false) ⇒ Object (also: #to_log_h)
- #to_s ⇒ Object
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(, 2_000) freeze end |
Instance Attribute Details
#category ⇒ Object (readonly)
Returns the value of attribute category.
49 50 51 |
# File 'lib/openai_compatible_errors/error.rb', line 49 def category @category end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
49 50 51 |
# File 'lib/openai_compatible_errors/error.rb', line 49 def code @code end |
#provider_message ⇒ Object (readonly)
Returns the value of attribute provider_message.
49 50 51 |
# File 'lib/openai_compatible_errors/error.rb', line 49 def @provider_message end |
#request_id ⇒ Object (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_ms ⇒ Object (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 |
#source ⇒ Object (readonly)
Returns the value of attribute source.
49 50 51 |
# File 'lib/openai_compatible_errors/error.rb', line 49 def source @source end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
49 50 51 |
# File 'lib/openai_compatible_errors/error.rb', line 49 def status @status end |
#type ⇒ Object (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.] end |
#hash ⇒ Object
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 |
#inspect ⇒ Object
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 |
#message ⇒ Object
65 66 67 |
# File 'lib/openai_compatible_errors/error.rb', line 65 def SAFE_MESSAGES.fetch(@category) end |
#retryable_category? ⇒ 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: , 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 && !@provider_message.nil? result[:provider_message] = @provider_message end result.freeze end |
#to_s ⇒ Object
112 113 114 |
# File 'lib/openai_compatible_errors/error.rb', line 112 def to_s end |