we-promise/sure · warning · BrexItem::AccountFlow::NoApiTokenError

BrexItem::AccountFlow::NoApiTokenError

Error message

BrexItem::AccountFlow::NoApiTokenError

What it means

Raised by BrexItem::AccountFlow#fetch_accounts as NoApiTokenError when brex_item.brex_provider is nil — no API token configured on the selected Brex connection, or no brex_item resolved at all (brex_item nil makes brex_item&.brex_provider nil too). Every public flow method (preload_payload, link_*, select_*) rescues it and converts it into a no_api_token state or navigation alert rather than letting it bubble.

Source

Thrown at app/models/brex_item/account_flow.rb:370

            names: result.already_linked_names.join(", ")
          )
        )
      else
        navigation(:new_account, :alert, I18n.t("brex_items.link_accounts.link_failed"))
      end
    end

    def navigation(target, flash_type, message)
      NavigationResult.new(target: target, flash_type: flash_type, message: message)
    end

    def cache_key
      self.class.cache_key(family, brex_item)
    end

    def fetch_accounts
      provider = brex_item&.brex_provider
      raise NoApiTokenError unless provider.present?

      accounts_data = provider.get_accounts
      accounts_data[:accounts] || []
    end

    def accounts
      cached_accounts = Rails.cache.read(cache_key)
      return cached_accounts unless cached_accounts.nil?

      fetch_and_cache_accounts
    end

    def fetch_and_cache_accounts
      available_accounts = fetch_accounts
      Rails.cache.write(cache_key, available_accounts, expires_in: CACHE_TTL)
      available_accounts
    end

View on GitHub (pinned to e69894adb9)

Solutions

  1. Complete token setup first: save the Brex API token on the BrexItem (settings → providers), then re-enter the flow.
  2. Guard the controller action: redirect unless brex_item&.credentials_configured? before rendering any flow that fetches accounts.
  3. Call the result-object wrappers (preload_payload / select_accounts_result / link_existing_account_result) instead of the bang internals — they translate NoApiTokenError into a structured no_api_token result.
  4. Expire the accounts cache (Rails.cache.delete(BrexItem::AccountFlow.cache_key(family, brex_item))) after changing the token so stale state does not mask the fix.

Example fix

# before
flow = BrexItem::AccountFlow.new(family: family, brex_item_id: params[:brex_item_id])
flow.preload_payload

# after — gate on credentials before any fetch
flow = BrexItem::AccountFlow.new(family: family, brex_item_id: params[:brex_item_id])
unless flow.selected? && flow.brex_item.credentials_configured?
  return redirect_to settings_providers_path, alert: "Connect your Brex account token first."
end
result = flow.preload_payload # => { success: false, error: "no_api_token", ... } if token missing
Defensive patterns

Strategy: validation

Validate before calling

flow = BrexItem::AccountFlow.new(family: family, brex_item_id: params[:brex_item_id])
return redirect_to(settings_providers_path, alert: "Add your Brex API token first") unless flow.selected? && flow.brex_item.credentials_configured?

Type guard

# Ruby
def brex_flow_ready?(flow)
  flow.selected? && flow.brex_item.respond_to?(:credentials_configured?) && flow.brex_item.credentials_configured?
end

Prevention

When it happens

Trigger: A user opens the Brex account flow before entering/storing their API token (brex_item.credentials_configured? false, see account_flow.rb:73), the token was blanked by an edit, or the flow was constructed with a brex_item_id that resolves to a deleted/missing item — then fetch_accounts hits `raise NoApiTokenError unless provider.present?` at app/models/brex_item/account_flow.rb:368-370.

Common situations: Bookmark/deep-link into the account picker before setup completes; settings form saved with empty token field; multi-connection families where the referenced item lost its credentials; tests instantiating AccountFlow on an item without a token.

Related errors


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