{"record":{"id":"71f0b16bca163b5e","repo":"we-promise/sure","slug":"brexitem-accountflow-invalidaccountnameerror","errorCode":null,"errorMessage":"BrexItem::AccountFlow::InvalidAccountNameError","messagePattern":"BrexItem::AccountFlow::InvalidAccountNameError","errorType":"exception","errorClass":"BrexItem::AccountFlow::InvalidAccountNameError","httpStatus":null,"severity":"warning","filePath":"app/models/brex_item/account_flow.rb","lineNumber":209,"sourceCode":"        created_accounts << account\n      end\n    end\n\n    brex_item.sync_later if created_accounts.any?\n\n    LinkAccountsResult.new(\n      created_accounts: created_accounts,\n      already_linked_names: already_linked_names,\n      invalid_account_ids: invalid_account_ids\n    )\n  end\n\n  def link_existing_account!(account:, brex_account_id:)\n    account_data = indexed_accounts[brex_account_id.to_s]\n    raise AccountNotFoundError unless account_data\n\n    account_name = BrexAccount.name_for(account_data)\n    raise InvalidAccountNameError if account_name.blank?\n\n    brex_account = nil\n\n    ActiveRecord::Base.transaction do\n      brex_account = upsert_brex_account!(brex_account_id, account_data)\n      raise AccountAlreadyLinkedError if brex_account.account_provider.present?\n\n      AccountProvider.create!(account: account, provider: brex_account)\n    end\n\n    brex_item.sync_later\n\n    brex_account\n  end\n\n  private\n\n    def selection_error_payload","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/we-promise/sure/blob/e69894adb92547273377398c15f45c979cd9416a/app/models/brex_item/account_flow.rb#L191-L227","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Name the account in the Brex dashboard first, then refresh the list (or expire the 5-minute cache) and link again.","If the payload shape changed, inspect the raw account_data (fetch via the provider) and update BrexAccount.name_for's field fallbacks.","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.","Pre-filter listable accounts by name presence so unnamed accounts never appear linkable."],"exampleFix":"# before\nflow.link_existing_account!(account: acct, brex_account_id: id)\n\n# after — exclude unnamed accounts from the picker\nlinkable = flow.preload_payload\n# render only accounts whose name resolves non-blank; on submit:\nbegin\n  flow.link_existing_account!(account: acct, brex_account_id: id)\nrescue BrexItem::AccountFlow::InvalidAccountNameError\n  redirect_to accounts_path, alert: \"Name this account in Brex first, then retry.\"\nend","handlingStrategy":"try-catch","validationCode":"# Only offer accounts whose name resolves before rendering the picker\nnamed = flow.preload_payload # renders only accounts with a non-blank BrexAccount.name_for value","typeGuard":"# Ruby\ndef linkable_brex_account?(account_data)\n  account_data.is_a?(Hash) && BrexAccount.name_for(account_data.with_indifferent_access).present?\nend","tryCatchPattern":"begin\n  flow.link_existing_account!(account: account, brex_account_id: id)\nrescue BrexItem::AccountFlow::InvalidAccountNameError\n  redirect_to accounts_path, alert: \"Name this account in Brex first, then retry linking.\"\nend","preventionTips":["Filter unnamed accounts out of any picker before the user can select them.","Name accounts in the Brex dashboard before linking; refresh (or wait out the 5-minute cache) afterwards.","Use the wrapper link_existing_account_result, which maps this error to an 'invalid account name' alert already."],"tags":["brex","account-linking","invalid-data","validation"],"backgroundTag":"invalid-account-name","analyzedSha":"e69894adb92547273377398c15f45c979cd9416a","analyzedAt":"2026-08-21T18:22:41.165Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}