{"record":{"id":"65436d4f0d7af066","repo":"garrytan/gstack","slug":"pdf-from-file-payloadpath-is-not-valid-json","errorCode":null,"errorMessage":"pdf: --from-file ${payloadPath} is not valid JSON (${msg}).","messagePattern":"pdf: --from-file (.+?) is not valid JSON \\((.+?)\\)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"browse/src/meta-commands.ts","lineNumber":158,"sourceCode":"export function parsePdfFromFile(payloadPath: string): ParsedPdfArgs {\n  // Parity with load-html --from-file (browse/src/write-commands.ts) and\n  // the direct load-html <file> path: every caller-supplied file path\n  // must pass validateReadPath so the safe-dirs policy can't be skirted\n  // by routing reads through the --from-file shortcut.\n  try {\n    validateReadPath(path.resolve(payloadPath));\n  } catch {\n    throw new Error(\n      `pdf: --from-file ${payloadPath} must be under ${SAFE_DIRECTORIES.join(' or ')} (security policy). Copy the payload into the project tree or /tmp first.`\n    );\n  }\n  const raw = fs.readFileSync(payloadPath, 'utf8');\n  let json: any;\n  try {\n    json = JSON.parse(raw);\n  } catch (err) {\n    const msg = err instanceof Error ? err.message : String(err);\n    throw new Error(`pdf: --from-file ${payloadPath} is not valid JSON (${msg}).`);\n  }\n  if (json === null || typeof json !== 'object' || Array.isArray(json)) {\n    throw new Error(`pdf: --from-file ${payloadPath} must be a JSON object, got ${Array.isArray(json) ? 'array' : typeof json}.`);\n  }\n  const out: ParsedPdfArgs = {\n    output: json.output || `${TEMP_DIR}/browse-page.pdf`,\n    format: json.format,\n    width: json.width,\n    height: json.height,\n    marginTop: json.marginTop,\n    marginRight: json.marginRight,\n    marginBottom: json.marginBottom,\n    marginLeft: json.marginLeft,\n    headerTemplate: json.headerTemplate,\n    footerTemplate: json.footerTemplate,\n    pageNumbers: json.pageNumbers === true,\n    tagged: json.tagged === true,\n    outline: json.outline === true,","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/garrytan/gstack/blob/94993f74012782fd94416dd44b8314f6363a13a4/browse/src/meta-commands.ts#L140-L176","documentation":"Thrown by parsePdfFromFile() when JSON.parse(raw) fails on the file contents. The error message includes the underlying parse error for diagnosis. The file passed path validation and was read successfully, but its content is not syntactically valid JSON.","triggerScenarios":"Pointing --from-file at a file that exists and is readable but contains malformed JSON — syntax errors, trailing commas, unquoted keys, single quotes, comments, or empty file.","commonSituations":"Hand-written JSON with trailing commas or comments (which are invalid in standard JSON), empty file, file saved with a BOM, or file partially written/corrupted.","solutions":["Validate the JSON file with a linter or 'jq .' before passing it","Remove trailing commas, comments, and ensure all keys and string values use double quotes","Check for empty or truncated files — ensure the write completed"],"exampleFix":"// before — pdf-config.json (invalid)\n{\n  \"format\": \"A4\",\n  \"printBackground\": true, // trailing comma + comment\n}\n\n// after — valid JSON\n{\n  \"format\": \"A4\",\n  \"printBackground\": true\n}","handlingStrategy":"validation","validationCode":"// Validate JSON before passing the file\nconst raw = fs.readFileSync(payloadPath, 'utf8');\ntry {\n  JSON.parse(raw);\n} catch (e) {\n  throw new Error(`Payload is not valid JSON: ${e.message}`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  const parsed = parsePdfFromFile(payloadPath);\n} catch (e) {\n  if (e.message.includes('not valid JSON')) {\n    console.error('Fix JSON syntax in', payloadPath);\n  }\n}","preventionTips":["Lint JSON files with 'jq .' or a JSON validator before using them as --from-file payloads","Avoid trailing commas, comments, and single quotes in JSON config files","Ensure file writes complete fully before referencing them"],"tags":["pdf","json","validation"],"backgroundTag":null,"analyzedSha":"94993f74012782fd94416dd44b8314f6363a13a4","analyzedAt":"2026-08-12T04:06:23.140Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}