Class: RubyLLM::Transport::ErrorMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/ruby_llm/transport/error_middleware.rb

Overview

:nodoc: all

Constant Summary collapse

CONTEXT_LENGTH_PATTERNS =
[
  /context length/i,
  /context window/i,
  /exceeds?.*context size/i,
  /maximum context/i,
  /request too large/i,
  /too many tokens/i,
  /token count exceeds/i,
  /input[_\s-]?token/i,
  /input or output tokens? must be reduced/i,
  /reduce the length of messages/i,
  /prompt is too long/i,
  /context limit/i
].freeze
RATE_LIMIT_PATTERNS =
[
  /rate limit/i,
  /per minute/i,
  /per hour/i,
  /per day/i
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ErrorMiddleware

Returns a new instance of ErrorMiddleware.



9
10
11
12
# File 'lib/ruby_llm/transport/error_middleware.rb', line 9

def initialize(app, options = {})
  super(app)
  @provider = options[:provider]
end

Class Method Details

.parse_error(provider:, response:) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ruby_llm/transport/error_middleware.rb', line 76

def parse_error(provider:, response:)
  message = provider&.parse_error(response)

  case response.status
  when 200..399
    message
  when 400
    raise ContextLengthExceededError.new(message, response:) if context_length_exceeded?(message)

    raise BadRequestError.new(message, response:)
  when 401
    raise UnauthorizedError.new(message, response:)
  when 402
    raise PaymentRequiredError.new(message, response:)
  when 403
    raise ForbiddenError.new(message, response:)
  when 429
    raise RateLimitError.new(message, response:) if rate_limited?(message)
    raise ContextLengthExceededError.new(message, response:) if context_length_exceeded?(message)

    raise RateLimitError.new(message, response:)
  when 500
    raise ServerError.new(message, response:)
  when 502..504
    raise ServiceUnavailableError.new(message, response:)
  when 529
    raise OverloadedError.new(message, response:)
  else
    raise Error.new(message, response:)
  end
end

Instance Method Details

#call(env) ⇒ Object

Sits directly above the adapter, inside the retry middleware, so this runs once per attempt: streaming state stored on the env by a previous attempt must not leak into the next one.



17
18
19
20
21
22
23
24
# File 'lib/ruby_llm/transport/error_middleware.rb', line 17

def call(env)
  env[:streaming_error_response] = nil
  env[:streaming_state] = nil
  @app.call(env).on_complete do |response|
    apply_retry_delay(response)
    self.class.parse_error(provider: @provider, response: streaming_error_response(response))
  end
end