Module: Ollama::Errors

Defined in:
lib/ollama/errors.rb

Overview

Maps HTTP-style responses into typed runtime errors.

Class Method Summary collapse

Class Method Details

.from_response(response, requested_model: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ollama/errors.rb', line 33

def self.from_response(response, requested_model: nil)
  status = response.code.to_i
  message = begin
    parsed = JSON.parse(response.body.to_s)
    parsed.is_a?(Hash) ? (parsed["error"] || parsed["message"] || response.message) : response.message
  rescue JSON::ParserError
    response.message
  end

  case status
  when 401
    UnauthorizedError.new("HTTP 401: #{message}", 401)
  when 404
    NotFoundError.new(message, requested_model: requested_model)
  when 429
    RateLimitError.new("HTTP 429: #{message}", 429)
  when 503
    ModelUnavailableError.new("HTTP 503: #{message}", 503)
  else
    HTTPError.new("HTTP #{status}: #{message}", status)
  end
end