Module: Ollama::HttpErrorHandler

Defined in:
lib/ollama/http_error_handler.rb

Overview

Shared HTTP error handling for all API clients

Instance Method Summary collapse

Instance Method Details

#extract_error_message(response) ⇒ String?

Extract error message from response body

Parameters:

  • response (Net::HTTPResponse)

    HTTP response

Returns:

  • (String, nil)

    Error message or nil



32
33
34
35
36
37
38
39
40
# File 'lib/ollama/http_error_handler.rb', line 32

def extract_error_message(response)
  body = response.body
  return nil if body.nil? || body.empty?

  parsed = JSON.parse(body)
  parsed["error"] if parsed.is_a?(Hash)
rescue JSON::ParserError
  nil
end

#handle_http_error(response, requested_model: nil) ⇒ Object

Handle HTTP error response and raise appropriate Ollama error

Parameters:

  • response (Net::HTTPResponse)

    HTTP response

  • requested_model (String, nil) (defaults to: nil)

    Model name for error context

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ollama/http_error_handler.rb', line 13

def handle_http_error(response, requested_model: nil)
  status_code = response.code.to_i
  error_message = extract_error_message(response) || response.message

  case status_code
  when 401
    raise UnauthorizedError.new("HTTP 401: #{error_message}", 401)
  when 404
    raise NotFoundError.new(error_message, requested_model: requested_model)
  when 503
    raise ModelUnavailableError.new("HTTP 503: #{error_message}", 503)
  else
    raise HTTPError.new("HTTP #{status_code}: #{error_message}", status_code)
  end
end