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

BrexItem::AccountFlow::InvalidAccountNameError

Error message

BrexItem::AccountFlow::InvalidAccountNameError

What it means

Raised by BrexItem::AccountFlow#link_existing_account! as InvalidAccountNameError when the Brex account data was found but BrexAccount.name_for(account_data) returns blank. Since a Maybe Account must be created with a name, linking is refused before any database write happens (the raise precedes the transaction).

Source

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

        created_accounts << account
      end
    end

    brex_item.sync_later if created_accounts.any?

    LinkAccountsResult.new(
      created_accounts: created_accounts,
      already_linked_names: already_linked_names,
      invalid_account_ids: invalid_account_ids
    )
  end

  def link_existing_account!(account:, brex_account_id:)
    account_data = indexed_accounts[brex_account_id.to_s]
    raise AccountNotFoundError unless account_data

    account_name = BrexAccount.name_for(account_data)
    raise InvalidAccountNameError if account_name.blank?

    brex_account = nil

    ActiveRecord::Base.transaction do
      brex_account = upsert_brex_account!(brex_account_id, account_data)
      raise AccountAlreadyLinkedError if brex_account.account_provider.present?

      AccountProvider.create!(account: account, provider: brex_account)
    end

    brex_item.sync_later

    brex_account
  end

  private

    def selection_error_payload

View on GitHub (pinned to e69894adb9)

Solutions

  1. Name the account in the Brex dashboard first, then refresh the list (or expire the 5-minute cache) and link again.
  2. If the payload shape changed, inspect the raw account_data (fetch via the provider) and update BrexAccount.name_for's field fallbacks.
  3. Rescue InvalidAccountNameError when calling link_existing_account! directly and prompt the user; the flow's link_existing_account_result already maps it to an 'invalid account name' alert.
  4. Pre-filter listable accounts by name presence so unnamed accounts never appear linkable.

Example fix

# before
flow.link_existing_account!(account: acct, brex_account_id: id)

# after — exclude unnamed accounts from the picker
linkable = flow.preload_payload
# render only accounts whose name resolves non-blank; on submit:
begin
  flow.link_existing_account!(account: acct, brex_account_id: id)
rescue BrexItem::AccountFlow::InvalidAccountNameError
  redirect_to accounts_path, alert: "Name this account in Brex first, then retry."
end
Defensive patterns

Strategy: try-catch

Validate before calling

# Only offer accounts whose name resolves before rendering the picker
named = flow.preload_payload # renders only accounts with a non-blank BrexAccount.name_for value

Type guard

# Ruby
def linkable_brex_account?(account_data)
  account_data.is_a?(Hash) && BrexAccount.name_for(account_data.with_indifferent_access).present?
end

Try / catch

begin
  flow.link_existing_account!(account: account, brex_account_id: id)
rescue BrexItem::AccountFlow::InvalidAccountNameError
  redirect_to accounts_path, alert: "Name this account in Brex first, then retry linking."
end

Prevention

When it happens

Trigger: The provider's get_accounts payload contains an account whose name-bearing fields are empty/missing — a new Brex account never named in the Brex dashboard, an API payload shape change emptying the name fields, or sanitized/blank names — so the check at app/models/brex_item/account_flow.rb:208-209 trips.

Common situations: Freshly created Brex accounts before the user names them; sandbox/demo data with sparse fields; upstream API version returning null name; localized accounts where name_for's fallbacks do not apply.

Related errors


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