{"record":{"id":"ae2106a2161df953","repo":"affaan-m/ECC","slug":"failed-to-read-label-error-message-ae2106","errorCode":null,"errorMessage":"Failed to read ${label}: ${error.message}","messagePattern":"Failed to read (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"scripts/lib/install-state.js","lineNumber":29,"sourceCode":"// bytes must be the installed bytes). install-state is validated by the\n// hand-rolled validator below, which enforces the same constraints as\n// schemas/install-state.schema.json (ecc.install.v1).\n\nlet cachedValidator = null;\n\nfunction cloneJsonValue(value) {\n  if (value === undefined) {\n    return undefined;\n  }\n\n  return JSON.parse(JSON.stringify(value));\n}\n\nfunction readJson(filePath, label) {\n  try {\n    return JSON.parse(fs.readFileSync(filePath, 'utf8'));\n  } catch (error) {\n    throw new Error(`Failed to read ${label}: ${error.message}`);\n  }\n}\n\nfunction getValidator() {\n  if (cachedValidator) {\n    return cachedValidator;\n  }\n\n  cachedValidator = createFallbackValidator();\n  return cachedValidator;\n}\n\nfunction createFallbackValidator() {\n  const validate = state => {\n    const errors = [];\n    validate.errors = errors;\n\n    function pushError(instancePath, message) {","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/scripts/lib/install-state.js#L11-L47","documentation":"readJson in scripts/lib/install-state.js wraps every failure of fs.readFileSync + JSON.parse of an install-state file (e.g. .claude/install-state.json) into a single uniform error. It conflates file-not-found, permission errors, and malformed JSON into one message, preserving the underlying Node error text.","triggerScenarios":"readInstallState/state is called and the state file is missing, unreadable (permissions), or contains invalid JSON — any readFileSync or JSON.parse throw is caught and re-thrown with the label prefixed.","commonSituations":"Running installer commands before the first install (state file does not exist yet); a crashed previous install left a truncated/partial state file; hand-editing the state JSON introduced a syntax error; restrictive file permissions after cloning on another machine.","solutions":["Check the underlying error in the message: ENOENT means the file is missing (run the install once to create it)","If the file exists, validate it with `node -e \"JSON.parse(require('fs').readFileSync(path,'utf8'))\"` and fix the syntax error","Fix file permissions if the message shows EACCES","If the state file is corrupt, delete it and re-run the installer to regenerate a fresh state"],"exampleFix":"// before: reading state from a script without guarding\nconst state = state();\n// after\nlet state;\ntry { state = state(); } catch (e) {\n  if (/ENOENT/.test(e.message)) state = undefined; // first run, no state yet\n  else throw e;\n}","handlingStrategy":"try-catch","validationCode":"// Pre-check before reading state\nconst statePath = '.claude/install-state.json';\nconst stateExists = fs.existsSync(statePath);\nconst stateReadable = stateExists && (() => { try { fs.accessSync(statePath, fs.constants.R_OK); return true; } catch { return false; } })();\nlet stateWellFormed = false;\nif (stateReadable) { try { JSON.parse(fs.readFileSync(statePath, 'utf8')); stateWellFormed = true; } catch {} }","typeGuard":null,"tryCatchPattern":"try {\n  const state = readInstallState();\n} catch (e) {\n  if (/Failed to read .*: ENOENT/.test(e.message)) {\n    // first run — treat as fresh install\n  } else if (/Failed to read .*: Unexpected token|JSON/.test(e.message)) {\n    // corrupt file — regenerate state\n  } else throw e;\n}","preventionTips":["Never hand-edit the install-state file; regenerate it via the installer","Treat a missing state file as a normal first-run condition, not a crash","Validate any external tooling that touches the state file with JSON.parse before writing","Check file permissions when installing in CI or shared environments"],"tags":["json","file-io","install-state"],"backgroundTag":"json-parse-error","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}