{"record":{"id":"26ed832c0898905b","repo":"paperclipai/paperclip","slug":"invalid-name-json-err-instanceof-error-err","errorCode":null,"errorMessage":"Invalid ${name} JSON: ${err instanceof Error ? err.message : String(err)}","messagePattern":"Invalid (.+?) JSON: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/commands/client/approval.ts","lineNumber":258,"sourceCode":"      }),\n  );\n}\n\nfunction parseCsv(value: string | undefined): string[] | undefined {\n  if (!value) return undefined;\n  const rows = value.split(\",\").map((v) => v.trim()).filter(Boolean);\n  return rows.length > 0 ? rows : undefined;\n}\n\nfunction parseJsonObject(value: string, name: string): Record<string, unknown> {\n  try {\n    const parsed = JSON.parse(value) as unknown;\n    if (typeof parsed !== \"object\" || parsed === null || Array.isArray(parsed)) {\n      throw new Error(`${name} must be a JSON object`);\n    }\n    return parsed as Record<string, unknown>;\n  } catch (err) {\n    throw new Error(`Invalid ${name} JSON: ${err instanceof Error ? err.message : String(err)}`);\n  }\n}\n","sourceCodeStart":240,"sourceCodeEnd":261,"githubUrl":"https://github.com/paperclipai/paperclip/blob/67001ec6eb96ae601aa27bc91d9b2415d665334a/cli/src/commands/client/approval.ts#L240-L261","documentation":"Catch-all thrown by parseJsonObject() in approval.ts for any error raised while parsing/validating an approval command's JSON option. It wraps JSON.parse SyntaxErrors (malformed JSON) AND the inner 'must be a JSON object' throw from [11]. The interpolated err.message tells you which sub-fault occurred.","triggerScenarios":"(a) --<name> value is malformed JSON (unbalanced braces, trailing comma, unquoted keys) → JSON.parse throws SyntaxError. (b) Valid JSON but not an object → the inner [11] throw is caught and rewrapped here as `Invalid <name> JSON: <name> must be a JSON object`.","commonSituations":"Hand-typing JSON on the command line (missing quotes, single quotes instead of double, trailing commas). Passing a YAML-ish value. Passing a JSON array where an object is required.","solutions":["Read the appended err.message: if it says 'Unexpected token', fix the JSON syntax; if it says 'must be a JSON object', wrap the value in {}.","Validate with jq before passing: `--<name> \"$(jq -c . input.json)\"`.","Avoid shell-quoting pitfalls by reading from a file if the JSON is non-trivial."],"exampleFix":"// before\npaperclipai approval ... --metadata {key:val}\n// after\npaperclipai approval ... --metadata '{\"key\":\"val\"}'","handlingStrategy":"validation","validationCode":"function parseJsonObjectSafe(value: string, name: string): Record<string, unknown> {\n  let parsed: unknown;\n  try { parsed = JSON.parse(value); }\n  catch (err) { throw new Error(`${name} is not valid JSON: ${err instanceof Error ? err.message : String(err)}`); }\n  if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {\n    throw new Error(`${name} must be a JSON object`);\n  }\n  return parsed as Record<string, unknown>;\n}","typeGuard":"function isPlainObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null && !Array.isArray(v);\n}","tryCatchPattern":"try { parseJsonObject(opts.metadata, 'metadata'); }\ncatch (err) {\n  const msg = err instanceof Error ? err.message : '';\n  if (msg.startsWith('Invalid metadata JSON:')) {\n    console.error(msg); // contains the underlying cause\n    process.exit(2);\n  }\n  throw err;\n}","preventionTips":["Use `jq -c .` to produce compact, validated JSON before passing it.","Read complex JSON from a file rather than inline shell strings.","Remember double quotes are required for JSON keys and string values.","Separate 'not JSON' from 'not an object' in your own parsers (this one conflates them)."],"tags":["cli","approval","json","validation"],"backgroundTag":null,"analyzedSha":"67001ec6eb96ae601aa27bc91d9b2415d665334a","analyzedAt":"2026-08-12T12:05:45.408Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}