Module: Supabase::Postgrest::RequestExec

Defined in:
lib/supabase/postgrest/request_builder.rb

Overview

Internal: shared execute helper (retry loop + error mapping).

Class Method Summary collapse

Class Method Details

.parse_error(response) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/supabase/postgrest/request_builder.rb', line 189

def parse_error(response)
  body = response.body
  parsed =
    begin
      body && !body.empty? ? JSON.parse(body) : nil
    rescue JSON::ParserError
      nil
    end

  Errors::APIError.new(parsed || Errors.generate_default_error_message(response))
end

.retry_delay(attempt) ⇒ Object



185
186
187
# File 'lib/supabase/postgrest/request_builder.rb', line 185

def retry_delay(attempt)
  [2**attempt, 30].min
end

.send_with_retry(request) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/supabase/postgrest/request_builder.rb', line 172

def send_with_retry(request)
  attempt = 0
  loop do
    extra = attempt.positive? ? { "X-Retry-Count" => attempt.to_s } : {}
    response = request.send_request(extra)
    return response if (200..299).include?(response.status)
    return response unless request.should_retry?(response, attempt)

    sleep(retry_delay(attempt))
    attempt += 1
  end
end