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

BrexItem::AccountFlow::NoApiTokenError

Error message

BrexItem::AccountFlow::NoApiTokenError

What it means

Raised by BrexItem::AccountFlow::Setup#import_accounts_from_api_if_needed when the Brex item has no usable API token. credentials_configured? (app/models/brex_item.rb:160) is true only when token.to_s.strip.present?, so a nil, empty, or whitespace-only token triggers this guard before any Brex API call is made. It is the module's way of refusing an account import on an uncredentialed item.

Source

Thrown at app/models/brex_item/account_flow/setup.rb:6

# frozen_string_literal: true

class BrexItem::AccountFlow
  module Setup
    def import_accounts_from_api_if_needed
      raise NoApiTokenError unless brex_item&.credentials_configured?

      available_accounts = fetch_accounts
      return nil if available_accounts.empty?

      existing_accounts = brex_item.brex_accounts.index_by(&:account_id)

      available_accounts.each do |account_data|
        account_id = account_data.with_indifferent_access[:id].to_s
        account_name = BrexAccount.name_for(account_data)
        next if account_id.blank? || account_name.blank?

        brex_account = existing_accounts[account_id]
        next if brex_account.present? && !brex_account_snapshot_changed?(brex_account, account_data)

        upsert_brex_account!(account_id, account_data)
      end

      nil

View on GitHub (pinned to e69894adb9)

Solutions

  1. Set a non-blank token on the item (brex_item.update!(token: <Brex API token>)) and retry the import
  2. Guard the call: return early unless brex_item&.credentials_configured? before invoking import_accounts_from_api_if_needed
  3. If driving the flow through AccountFlow public methods, rely on the existing NoApiTokenError rescues (app/models/brex_item/account_flow.rb:80,116,134,280) which convert it to a 'no credentials' status instead of an exception

Example fix

// before
flow.import_accounts_from_api_if_needed # raises BrexItem::AccountFlow::NoApiTokenError

// after
return unless brex_item&.credentials_configured?

flow.import_accounts_from_api_if_needed
Defensive patterns

Strategy: validation

Validate before calling

brex_item&.credentials_configured? # => false means import_accounts_from_api_if_needed will raise NoApiTokenError

Try / catch

rescue BrexItem::AccountFlow::NoApiTokenError and treat as a setup-required state (prompt for the Brex token), not as a sync failure

Prevention

When it happens

Trigger: Calling import_accounts_from_api_if_needed (directly or via an AccountFlow wrapper) on a BrexItem whose token attribute is blank: a newly created item where the token was never entered, a token that normalization (normalize_token strips whitespace) reduced to empty, or a record restored without its token column.

Common situations: Setup flows that kick off an import before credentials are saved; console/test runs with factory records lacking tokens; environments where the token did not survive a data copy; token revoked and field cleared while scheduled flows still run.

Related errors


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