{"record":{"id":"dba6b8a33272826c","repo":"jlcodes99/cockpit-tools","slug":"errorcode-parsejsonorthrow-caller-supplied","errorCode":null,"errorMessage":"errorCode (parseJsonOrThrow caller-supplied)","messagePattern":"errorCode \\(parseJsonOrThrow caller-supplied\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/services/accountTransferService.ts","lineNumber":204,"sourceCode":"  processed_accounts: number;\n  imported_accounts: number;\n  current_platform: PlatformId | null;\n  details: AccountTransferImportProgressDetail[];\n}\n\nexport interface AccountTransferImportOptions {\n  onProgress?: (progress: AccountTransferImportProgress) => void;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n  return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\nfunction parseJsonOrThrow(json: string, errorCode: string): unknown {\n  try {\n    return JSON.parse(json) as unknown;\n  } catch {\n    throw new Error(errorCode);\n  }\n}\n\nfunction normalizeAccountIds(accounts: AccountWithId[]): string[] {\n  return accounts\n    .map((account) => account.id)\n    .filter((id): id is string => typeof id === 'string' && id.trim().length > 0);\n}\n\nfunction resolvePlatformPayload(rawSection: unknown): AccountTransferPlatformPayload | null {\n  if (rawSection === undefined) return null;\n  if (rawSection === null) {\n    return {\n      account_count: 0,\n      exported_data: [],\n    };\n  }\n","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/jlcodes99/cockpit-tools/blob/1ed8b77992d62ca81fabf744deb0839ad361d5bf/src/services/accountTransferService.ts#L186-L222","documentation":"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'.","triggerScenarios":"JSON.parse throws on the input string passed with an errorCode; the thrown Error's message is exactly the caller-provided code.","commonSituations":"Importing a truncated or hand-edited transfer bundle, non-JSON content (e.g. HTML error page) pasted into an import dialog, wrong file encoding.","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"],"exampleFix":"// before\nconst data = parseJsonOrThrow(userPastedText, 'invalid_json');\n// after\nlet parsed;\ntry { parsed = JSON.parse(userPastedText); } catch (e) { alert('File is not valid JSON: ' + e.message); return; }","handlingStrategy":"validation","validationCode":"function isValidJson(s: string): boolean {\n  try { JSON.parse(s); return true; } catch { return false; }\n}\n// if (!isValidJson(text)) showImportError('invalid_json');","typeGuard":"function isRecord(value: unknown): value is Record<string, unknown> {\n  return typeof value === 'object' && value !== null && !Array.isArray(value);\n}","tryCatchPattern":"try {\n  const data = parseJsonOrThrow(text, 'invalid_json');\n  ...\n} catch (e) {\n  if (e instanceof Error && e.message === 'invalid_json') {\n    alert('The file is not valid JSON.');\n  } else throw e;\n}","preventionTips":["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"],"tags":["json","parsing","serialization"],"backgroundTag":"invalid-json","analyzedSha":"1ed8b77992d62ca81fabf744deb0839ad361d5bf","analyzedAt":"2026-09-05T09:51:41.178Z","contentChangedAt":"2026-09-05T09:51:41.178Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}