jlcodes99/cockpit-tools · error

failures.join("; ") || "无法解析导入内容"

Error message

failures.join("; ") || "无法解析导入内容"

What it means

When importing Codex accounts, each record is parsed in a loop; per-record errors are collected into a failures array. If nothing was successfully imported (imported.length === 0), the controller throws an Error whose message is all failures joined with '; ', or the fallback Chinese text '无法解析导入内容' ('unable to parse imported content') when failures is somehow empty. The error is an aggregate report of why every record failed.

Source

Thrown at src/pages/useCodexAccountsAccessController.tsx:2634

          page.setAddMessage(
            t("common.shared.externalImport.statusImporting", {
              current,
              total: payloads.length,
              defaultValue: "正在导入第 {{current}} / {{total}} 个账号",
            }),
          );
          try {
            imported.push(
              ...(await codexService.importCodexFromJson(payloads[index])),
            );
          } catch (error) {
            failures.push(
              `${current}: ${String(error).replace(/^Error:\s*/, "")}`,
            );
          }
        }
        if (imported.length === 0) {
          throw new Error(failures.join("; ") || "无法解析导入内容");
        }
        // 待授权账号若带 2FA 秘钥,同步写入本地 MFA 速查
        for (const account of imported) {
          const secret = account.two_factor_secret?.trim();
          if (!secret) continue;
          setSavedMfaRecords(
            upsertSavedMfaRecord({
              secret,
              accountName: account.email,
              remark: account.account_note,
            }),
          );
        }
        await fetchAccounts();
        await assignCodexAccountsToTargetGroup(imported);
        if (imported.length > 0) {
          await emitAccountsChanged({
            platformId: "codex",

View on GitHub (pinned to 1ed8b77992)

Solutions

  1. Read the joined failure list in the message: each 'source: reason' entry points at the exact offending record and cause.
  2. Fix or remove the invalid records in the import file and re-import (correct field names, valid JSON, required fields like id/token present).
  3. Validate the file before import (JSON.parse plus a schema check per record) and pre-filter unparseable entries.
  4. If the file is empty or entirely unreadable, regenerate the export from the source tool before importing.
  5. Import incrementally (one or two records) to isolate which record shape the importer rejects.

Example fix

// before
// import file record missing required field
{ "name": "acct1" }
// after
{ "id": "acct1", "name": "acct1", "token": "..." }  // required fields present
Defensive patterns

Strategy: try-catch

Validate before calling

const validateImport = (records: unknown[]) =>
  records.map((r, i) => {
    const ok =
      typeof r === 'object' && r !== null && 'id' in r;
    return ok ? null : `record ${i}: missing required fields`;
  }).filter(Boolean);

Type guard

const isImportableAccount = (r: unknown): r is { id: string; [k: string]: unknown } =>
  typeof r === 'object' && r !== null &&
  typeof (r as { id?: unknown }).id === 'string' &&
  (r as { id: string }).id.length > 0;

Try / catch

try {
  await importAccounts(file);
} catch (e) {
  const msg = String(e).replace(/^Error:\s*/, '');
  if (msg.includes('无法解析导入内容') || msg.includes('; ')) {
    // aggregate failure list — show per-record reasons
    showImportReport(msg.split('; '));
  } else throw e;
}

Prevention

When it happens

Trigger: Importing a file/payload where every account record fails parsing/validation (e.g. malformed JSON entries, missing required fields, duplicate ids rejected) so imported stays empty; the thrown message lists each '<source>: <underlying error>' pair.

Common situations: Importing an accounts export from another tool or older version with a different schema; uploading a hand-edited JSON/YAML with syntax or field-name mistakes; importing an empty file so no records parse and the fallback message is used.

Related errors


AI-assisted analysis of jlcodes99/cockpit-tools@1ed8b77992 (2026-09-05). Data as JSON: /api/errors/8aa8e37da8ea6b2d. Report an issue: GitHub.