we-promise/sure · error · Provider::Brex::BrexError

pagination_error

pagination_error

Error message

Brex pagination exceeded #{MAX_PAGES} pages

What it means

StandardError raised by SnaptradeItem#import_latest_snaptrade_data when snaptrade_provider (resolved via SnaptradeItem::Provided, returning nil when OAuth credentials are absent) is nil. The item model logs 'OAuth not authorized' then raises with the generic user-facing message; the rescue re-logs and re-raises. In practice this means oauth_access_token is missing — the SnapTrade brokerage link was never completed or its tokens were cleared.

Source

Thrown at app/models/provider/brex.rb:134

      { amount: total, currency: currency }
    end

    def posted_at_start_params(start_date)
      return {} if start_date.blank?

      { posted_at_start: rfc3339_start_date(start_date) }
    end

    def get_paginated(path, params: {})
      records = []
      cursor = nil
      seen_cursors = Set.new
      page_count = 0

      loop do
        page_count += 1
        raise BrexError.new("Brex pagination exceeded #{MAX_PAGES} pages", :pagination_error) if page_count > MAX_PAGES

        page_params = params.compact.merge(limit: DEFAULT_LIMIT)
        page_params[:cursor] = cursor if cursor.present?

        response_payload = get_json(path, params: page_params)
        if response_payload.is_a?(Array)
          records.concat(response_payload)
          break
        end

        page_records = extract_records(response_payload)
        records.concat(page_records)

        next_cursor = response_payload.with_indifferent_access[:next_cursor]
        break if next_cursor.blank?

        if seen_cursors.include?(next_cursor)
          raise BrexError.new("Brex pagination returned a repeated cursor", :pagination_error)

View on GitHub (pinned to e69894adb9)

Solutions

  1. Complete SnapTrade authorization (connection portal flow) so oauth_access_token is stored
  2. Verify item.oauth_configured? (oauth_access_token.present?) before starting the sync job
  3. Cancel or skip scheduled jobs for items with missing OAuth tokens
  4. If tokens were intentionally revoked, destroy the item (destroy_later) instead of leaving it syncable

Example fix

# before
SnaptradeItem.find(id).import_latest_snaptrade_data(sync: sync)

# after
item = SnaptradeItem.find(id)
unless item.oauth_configured?
  Rails.logger.info("Skipping SnaptradeItem #{id}: OAuth not authorized")
  next # or return
end
item.import_latest_snaptrade_data(sync: sync)
Defensive patterns

Strategy: validation

Validate before calling

return unless item.oauth_configured? # oauth_access_token.present?

Try / catch

begin
  item.import_latest_snaptrade_data(sync: sync)
rescue StandardError => e
  raise unless e.message == "SnapTrade is not authorized"
  sync.update!(status: :error, status_text: "SnapTrade not connected — reconnect required")
end

Prevention

When it happens

Trigger: A sync job running for a SnaptradeItem whose OAuth flow never finished (user abandoned the connection portal); tokens revoked/deleted; item destroyed but a queued sync job still executes; calling import_latest_snaptrade_data directly in console/rake for an unlinked item.

Common situations: User starts SnapTrade linking, closes the portal mid-flow, and a welcome/initial sync fires anyway; cleanup of OAuth tokens without cancelling scheduled jobs; staging seeds creating items without auth.

Related errors


AI-assisted analysis of we-promise/sure@e69894adb9 (2026-08-21). Data as JSON: /api/errors/e9f91b51674d4bd5. Report an issue: GitHub.