jlcodes99/cockpit-tools · error

invalid_backup_json

invalid_backup_json

Error message

invalid_backup_json

What it means

resolveBackupPlatformPayload parses a scheduled-backup file and requires the top level (or parsed.accounts) to be a JSON object. If JSON.parse succeeded but produced a non-object (array, string, number) — or the string was not valid JSON at all, in which case JSON.parse throws SyntaxError before this check — the library throws invalid_backup_json because there is no backup bundle to extract a platform payload from.

Source

Thrown at src/services/scheduledBackupService.ts:299

  const deleted = await cleanupAutoBackupFilesInternal(retentionDays);
  if (deleted.length > 0) {
    dispatchAutoBackupStateChanged();
  }
  return deleted;
}

export async function openAutoBackupDir(): Promise<void> {
  await invoke('open_auto_backup_dir');
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === 'object' && value !== null && !Array.isArray(value);
}

function resolveBackupPlatformPayload(jsonContent: string, platform: PlatformId): unknown {
  const parsed = JSON.parse(jsonContent) as unknown;
  if (!isRecord(parsed)) {
    throw new Error('invalid_backup_json');
  }
  const accountBundle = isRecord(parsed.accounts) ? parsed.accounts : parsed;
  if (!isRecord(accountBundle.platforms)) {
    throw new Error('backup_accounts_missing');
  }
  const payload = accountBundle.platforms[platform];
  if (!isRecord(payload)) {
    throw new Error('backup_platform_missing');
  }
  return payload.exported_data ?? payload.data ?? payload.accounts ?? [];
}

export function extractAutoBackupPlatformJson(jsonContent: string, platform: PlatformId): string {
  const payload = resolveBackupPlatformPayload(jsonContent, platform);
  return JSON.stringify(payload, null, 2);
}

export function normalizeAutoBackupPlatforms(

View on GitHub (pinned to 1ed8b77992)

Solutions

  1. JSON.parse the file yourself first and confirm the result is a non-null, non-array object before calling the API
  2. Re-generate the backup via createManagedBackup to replace the corrupted file
  3. Validate the file was fully written (compare size/checksum) if it came from a backup job or sync service
  4. Verify restore is pointed at the auto-backup file, not an intermediate or unrelated export

Example fix

// before
const data = await payload(backuptext, 'qoder');
// after
let parsed: unknown;
try { parsed = JSON.parse(backupText); } catch { throw new Error('file is not valid JSON'); }
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
  throw new Error('backup file is not a JSON object');
}
const data = await payload(backupText, 'qoder');
Defensive patterns

Strategy: validation

Validate before calling

let parsed: unknown;
try { parsed = JSON.parse(fileText); } catch { throw new Error('Not valid JSON'); }
const isRecord = (v: unknown): v is Record<string, unknown> =>
  typeof v === 'object' && v !== null && !Array.isArray(v);
if (!isRecord(parsed)) throw new Error('Backup must be a JSON object');

Type guard

const isRecord = (v: unknown): v is Record<string, unknown> =>
  typeof v === 'object' && v !== null && !Array.isArray(v);

Try / catch

try {
  const data = await payload(backupText, platform);
} catch (e) {
  if (['invalid_backup_json','backup_accounts_missing','backup_platform_missing'].includes(e.message)) {
    promptRestoreFromDifferentFile(e.message);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling payload/extractAutoBackupPlatformJson with a file whose content is not a JSON object: an empty file, a truncated write, an array export, or a non-JSON (e.g. HTML error page) file passed as jsonContent.

Common situations: A backup file truncated by disk-full or process kill mid-write, user selects the wrong file (log or export list instead of backup), or a sync tool replaced the file with an HTML error page.

Related errors


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