jlcodes99/cockpit-tools · error
errorCode (parseJsonOrThrow caller-supplied)
Error message
errorCode (parseJsonOrThrow caller-supplied)
What it means
parseJsonOrThrow wraps JSON.parse failures by throwing the caller-supplied errorCode string verbatim instead of a SyntaxError. It lets call sites normalize malformed-JSON failures into stable error codes such as 'invalid_json'.
Source
Thrown at src/services/accountTransferService.ts:204
processed_accounts: number;
imported_accounts: number;
current_platform: PlatformId | null;
details: AccountTransferImportProgressDetail[];
}
export interface AccountTransferImportOptions {
onProgress?: (progress: AccountTransferImportProgress) => void;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function parseJsonOrThrow(json: string, errorCode: string): unknown {
try {
return JSON.parse(json) as unknown;
} catch {
throw new Error(errorCode);
}
}
function normalizeAccountIds(accounts: AccountWithId[]): string[] {
return accounts
.map((account) => account.id)
.filter((id): id is string => typeof id === 'string' && id.trim().length > 0);
}
function resolvePlatformPayload(rawSection: unknown): AccountTransferPlatformPayload | null {
if (rawSection === undefined) return null;
if (rawSection === null) {
return {
account_count: 0,
exported_data: [],
};
}
View on GitHub (pinned to 1ed8b77992)
Solutions
- Validate the JSON with JSON.parse (or a JSON linter) before importing and fix the syntax error it reports
- Re-export a fresh transfer bundle from a working instance
- Ensure the file is read as UTF-8 text with no BOM or added whitespace
- Catch the thrown code ('invalid_json') in the UI and show a clear 'not valid JSON' message
Example fix
// before
const data = parseJsonOrThrow(userPastedText, 'invalid_json');
// after
let parsed;
try { parsed = JSON.parse(userPastedText); } catch (e) { alert('File is not valid JSON: ' + e.message); return; } Defensive patterns
Strategy: validation
Validate before calling
function isValidJson(s: string): boolean {
try { JSON.parse(s); return true; } catch { return false; }
}
// if (!isValidJson(text)) showImportError('invalid_json'); Type guard
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
} Try / catch
try {
const data = parseJsonOrThrow(text, 'invalid_json');
...
} catch (e) {
if (e instanceof Error && e.message === 'invalid_json') {
alert('The file is not valid JSON.');
} else throw e;
} Prevention
- Validate pasted/loaded content with JSON.parse before running business parsing
- Read files as UTF-8 text and strip BOM
- Re-export bundles from the app instead of hand-editing them
- Surface SyntaxError details to the user during import
When it happens
Trigger: JSON.parse throws on the input string passed with an errorCode; the thrown Error's message is exactly the caller-provided code.
Common situations: Importing a truncated or hand-edited transfer bundle, non-JSON content (e.g. HTML error page) pasted into an import dialog, wrong file encoding.
Related errors
- invalidJsonMessage
- messages.invalidJson
- messages.empty
- [AccountGroups] Failed to load groups: ${String(error)}
- invalid_bundle_root
AI-assisted analysis of jlcodes99/cockpit-tools@1ed8b77992 (2026-09-05).
Data as JSON: /api/errors/dba6b8a33272826c.
Report an issue: GitHub.