{"record":{"id":"881340a979520cd4","repo":"Crosstalk-Solutions/project-nomad","slug":"unexpected-fda-manifest-format-missing-or-invalid-881340","errorCode":null,"errorMessage":"Unexpected FDA manifest format: missing or invalid \"total_records\"","messagePattern":"Unexpected FDA manifest format: missing or invalid \"total_records\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"admin/util/drug_labels.ts","lineNumber":268,"sourceCode":"\n  const drug = results['drug'] as Record<string, unknown> | undefined\n  if (!drug || typeof drug !== 'object') {\n    throw new Error('Unexpected FDA manifest format: missing \"results.drug\"')\n  }\n\n  const label = drug['label'] as Record<string, unknown> | undefined\n  if (!label || typeof label !== 'object') {\n    throw new Error('Unexpected FDA manifest format: missing \"results.drug.label\"')\n  }\n\n  const export_date = label['export_date']\n  if (typeof export_date !== 'string' || export_date.trim() === '') {\n    throw new Error('Unexpected FDA manifest format: missing or invalid \"export_date\"')\n  }\n\n  const total_records = label['total_records']\n  if (typeof total_records !== 'number') {\n    throw new Error('Unexpected FDA manifest format: missing or invalid \"total_records\"')\n  }\n\n  const rawPartitions = label['partitions']\n  if (!Array.isArray(rawPartitions) || rawPartitions.length === 0) {\n    throw new Error(\n      'Unexpected FDA manifest format: \"partitions\" is missing or empty'\n    )\n  }\n\n  const partitions: DrugLabelPartition[] = []\n  for (const p of rawPartitions as unknown[]) {\n    if (typeof p !== 'object' || p === null) {\n      process.stderr.write('[parseDrugLabelManifest] Skipping non-object partition\\n')\n      continue\n    }\n    const part = p as Record<string, unknown>\n    if (typeof part['file'] !== 'string' || (part['file'] as string).trim() === '') {\n      process.stderr.write(","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/Crosstalk-Solutions/project-nomad/blob/0bd1c6f4f9888d577fe232de06ac144bb8337131/admin/util/drug_labels.ts#L250-L286","documentation":"Thrown by parseDrugLabelManifest after export_date validation passes: label['total_records'] must be typeof 'number'. Any other type (missing, string, null) aborts manifest parsing with this error. It guards downstream consumers that rely on total_records for progress reporting and record counting.","triggerScenarios":"The FDA manifest's results.drug.label object contains total_records as a JSON string (\"42000\" instead of 42000), the field is omitted, or an intermediate proxy/re-serializer converts numbers to strings (e.g. some JSON transform pipelines or hand-edited fixtures).","commonSituations":"Upstream API starts serializing counts as strings; test fixtures hand-written with quoted numbers; a caching layer that JSON round-trips values through a form that loses number typing; API version mismatch.","solutions":["Dump the manifest and check typeof results.drug.label.total_records in the actual response.","If the API now returns a numeric string, coerce defensively: Number(...) with NaN check inside parseDrugLabelManifest.","Update test fixtures to match the real API shape.","Confirm you are hitting the documented FDA bulk-download endpoint version."],"exampleFix":"// before\nconst total_records = label['total_records']\nif (typeof total_records !== 'number') {\n  throw new Error('Unexpected FDA manifest format: missing or invalid \"total_records\"')\n}\n// after\nconst raw = label['total_records']\nconst total_records = typeof raw === 'number' ? raw : typeof raw === 'string' && /^\\d+$/.test(raw) ? Number(raw) : NaN\nif (!Number.isFinite(total_records)) {\n  throw new Error(`Unexpected FDA manifest format: \"total_records\" was ${JSON.stringify(raw)}`)\n}","handlingStrategy":"validation","validationCode":"const tr = label?.['total_records']\nif (typeof tr !== 'number' && !(typeof tr === 'string' && /^\\d+$/.test(tr))) {\n  // refuse to parse; report schema problem\n}","typeGuard":"function isValidTotalRecords(v: unknown): boolean {\n  return typeof v === 'number' && Number.isFinite(v) && v >= 0\n}","tryCatchPattern":"try { parseDrugLabelManifest(json) } catch (e) { if (e.message.includes('total_records')) reportSchemaDrift(json); throw e }","preventionTips":["Add fixture tests asserting total_records is numeric in the current API","Coerce numeric strings at the boundary if the upstream is known to stringify","Monitor FDA API changelog before upgrading the integration"],"tags":["fda","manifest","schema-validation","typescript"],"backgroundTag":"schema-validation-failed","analyzedSha":"0bd1c6f4f9888d577fe232de06ac144bb8337131","analyzedAt":"2026-08-27T05:34:15.424Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}