From 6dd65918b462aa1fb5ef066286970a8c0e727e29 Mon Sep 17 00:00:00 2001 From: hobostay Date: Sun, 29 Mar 2026 20:51:23 +0800 Subject: [PATCH] Fix: Improve error handling in readConfig() to distinguish read vs parse errors Previously, readConfig() would report all errors as "failed to parse", even if the actual error was a file system issue (permission denied, file is a directory, etc.) during readFileSync(). This change splits the error handling into two stages: 1. First, read the file content - errors are reported as "failed to read" 2. Then, parse the JSON - errors are reported as "failed to parse" This provides more accurate error messages to help users diagnose configuration issues. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/config.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index ddf04e0..1f28b57 100644 --- a/src/config.ts +++ b/src/config.ts @@ -22,8 +22,17 @@ export type LatConfig = { export function readConfig(): LatConfig { const configPath = getConfigPath(); if (!existsSync(configPath)) return {}; + let content: string; try { - return JSON.parse(readFileSync(configPath, 'utf-8')); + content = readFileSync(configPath, 'utf-8'); + } catch (err) { + process.stderr.write( + `Error: failed to read config ${configPath}: ${(err as Error).message}\n`, + ); + process.exit(1); + } + try { + return JSON.parse(content); } catch (err) { process.stderr.write( `Error: failed to parse config ${configPath}: ${(err as Error).message}\n`,