Class: RubyLLM::Transport::ErrorMiddleware
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- RubyLLM::Transport::ErrorMiddleware
- 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
-
#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.
-
#initialize(app, options = {}) ⇒ ErrorMiddleware
constructor
A new instance of ErrorMiddleware.
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, = {}) super(app) @provider = [: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:) = provider&.parse_error(response) case response.status when 200..399 when 400 raise ContextLengthExceededError.new(, response:) if context_length_exceeded?() raise BadRequestError.new(, response:) when 401 raise UnauthorizedError.new(, response:) when 402 raise PaymentRequiredError.new(, response:) when 403 raise ForbiddenError.new(, response:) when 429 raise RateLimitError.new(, response:) if rate_limited?() raise ContextLengthExceededError.new(, response:) if context_length_exceeded?() raise RateLimitError.new(, response:) when 500 raise ServerError.new(, response:) when 502..504 raise ServiceUnavailableError.new(, response:) when 529 raise OverloadedError.new(, response:) else raise Error.new(, 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 |