{"record":{"id":"6e34b3701258dae7","repo":"we-promise/sure","slug":"brexitem-accountflow-noapitokenerror-6e34b3","errorCode":null,"errorMessage":"BrexItem::AccountFlow::NoApiTokenError","messagePattern":"BrexItem::AccountFlow::NoApiTokenError","errorType":"exception","errorClass":"BrexItem::AccountFlow::NoApiTokenError","httpStatus":null,"severity":"error","filePath":"app/models/brex_item/account_flow/setup.rb","lineNumber":6,"sourceCode":"# frozen_string_literal: true\n\nclass BrexItem::AccountFlow\n  module Setup\n    def import_accounts_from_api_if_needed\n      raise NoApiTokenError unless brex_item&.credentials_configured?\n\n      available_accounts = fetch_accounts\n      return nil if available_accounts.empty?\n\n      existing_accounts = brex_item.brex_accounts.index_by(&:account_id)\n\n      available_accounts.each do |account_data|\n        account_id = account_data.with_indifferent_access[:id].to_s\n        account_name = BrexAccount.name_for(account_data)\n        next if account_id.blank? || account_name.blank?\n\n        brex_account = existing_accounts[account_id]\n        next if brex_account.present? && !brex_account_snapshot_changed?(brex_account, account_data)\n\n        upsert_brex_account!(account_id, account_data)\n      end\n\n      nil","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/we-promise/sure/blob/e69894adb92547273377398c15f45c979cd9416a/app/models/brex_item/account_flow/setup.rb#L1-L24","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set a non-blank token on the item (brex_item.update!(token: <Brex API token>)) and retry the import","Guard the call: return early unless brex_item&.credentials_configured? before invoking import_accounts_from_api_if_needed","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"],"exampleFix":"// before\nflow.import_accounts_from_api_if_needed # raises BrexItem::AccountFlow::NoApiTokenError\n\n// after\nreturn unless brex_item&.credentials_configured?\n\nflow.import_accounts_from_api_if_needed","handlingStrategy":"validation","validationCode":"brex_item&.credentials_configured? # => false means import_accounts_from_api_if_needed will raise NoApiTokenError","typeGuard":null,"tryCatchPattern":"rescue BrexItem::AccountFlow::NoApiTokenError and treat as a setup-required state (prompt for the Brex token), not as a sync failure","preventionTips":["Save the Brex token before triggering any account import flow","Gate import buttons/actions on item.credentials_configured?","In background flows, rely on AccountFlow's existing NoApiTokenError rescues that return a 'no credentials' status"],"tags":["brex","api-token","credentials","account-import"],"backgroundTag":"missing-credentials","analyzedSha":"e69894adb92547273377398c15f45c979cd9416a","analyzedAt":"2026-08-21T18:22:41.165Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}