{"record":{"id":"042ef9e0360cec9e","repo":"we-promise/sure","slug":"brexitem-accountflow-accountnotfounderror","errorCode":null,"errorMessage":"BrexItem::AccountFlow::AccountNotFoundError","messagePattern":"BrexItem::AccountFlow::AccountNotFoundError","errorType":"exception","errorClass":"BrexItem::AccountFlow::AccountNotFoundError","httpStatus":null,"severity":"warning","filePath":"app/models/brex_item/account_flow.rb","lineNumber":206,"sourceCode":"        )\n\n        AccountProvider.create!(account: account, provider: brex_account)\n        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","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/we-promise/sure/blob/e69894adb92547273377398c15f45c979cd9416a/app/models/brex_item/account_flow.rb#L188-L224","documentation":"Raised by BrexItem::AccountFlow#link_existing_account! as AccountNotFoundError when brex_account_id is absent from indexed_accounts, the index built from the provider's get_accounts (served from a 5-minute Rails cache keyed brex_accounts_<family>_<item>). The id must exist in the currently fetched (or cached) Brex account list at link time.","triggerScenarios":"POSTing link_existing_account with a brex_account_id that is not in the list: a stale page rendered from cache before the account was closed at Brex or before the token switched to a different Brex organization; a hand-edited id; race where another flow expired/rewrote the cache between render and submit (app/models/brex_item/account_flow.rb:205-206).","commonSituations":"User keeps an old tab open, closes the account at Brex, then submits; switching between multiple Brex connections (cache is per family+item); id passed as the internal BrexAccount record id instead of the provider's account id.","solutions":["Refresh the account list and retry with an id returned by the current flow (select_existing_account_result / preload_payload) — pass the provider account id, not the local DB id.","Expire the stale cache: Rails.cache.delete(BrexItem::AccountFlow.cache_key(family, brex_item)) (TTL is 5 minutes, so waiting also works).","Confirm the correct Brex connection (brex_item) is selected when the family has several — ids do not transfer between them.","Rescue AccountNotFoundError when calling link_existing_account! directly and re-render the picker with fresh data."],"exampleFix":"# before\nflow.link_existing_account!(account: account, brex_account_id: params[:brex_account_id])\n\n# after — validate against the live list, and handle the miss\nbegin\n  flow.link_existing_account!(account: account, brex_account_id: params[:brex_account_id])\nrescue BrexItem::AccountFlow::AccountNotFoundError\n  Rails.cache.delete(BrexItem::AccountFlow.cache_key(family, flow.brex_item))\n  redirect_to select_accounts_path, alert: \"That Brex account is no longer available — pick again.\"\nend","handlingStrategy":"try-catch","validationCode":"# expire stale cache so the id list is fresh before linking\nRails.cache.delete(BrexItem::AccountFlow.cache_key(family, brex_item))\nflow = BrexItem::AccountFlow.new(family: family, brex_item: brex_item)","typeGuard":null,"tryCatchPattern":"begin\n  flow.link_existing_account!(account: account, brex_account_id: id)\nrescue BrexItem::AccountFlow::AccountNotFoundError\n  redirect_to select_accounts_path, alert: \"That Brex account is no longer available — choose again.\"\nend","preventionTips":["Always submit provider account ids taken from a freshly rendered list, not from stale tabs or hand-copied values.","Prefer the result wrapper link_existing_account_result, which already maps AccountNotFoundError to an alert navigation.","Remember the accounts cache lives 5 minutes per family+item — expire it after switching tokens or connections."],"tags":["brex","account-linking","not-found","stale-cache"],"backgroundTag":"account-not-found","analyzedSha":"e69894adb92547273377398c15f45c979cd9416a","analyzedAt":"2026-08-21T18:22:41.165Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}