Isolate malformed enterprise config files instead of failing every command
loadCorpConfigs parsed every file with a bare Schema.parse, so one hand- edited JSON aborted the whole directory: every corps/rules/webhook command died with a raw parse error and no hint which file was at fault. Each file now parses inside its own try/catch — bad files are skipped and reported once per process on stderr with the offending path; the remaining enterprises stay usable. loadGlobalConfig falls back to defaults (warning on stderr) so config paths / corps list keep working for diagnosis. doctor gains a config_parseability check that names unparseable files.
This commit is contained in:
1 parent
66eb81f3d1
commit
b5d1bbda15
4 files changed
+144
-5
No files matched your search
@@ -85,20 +85,59 @@ export function syncLegacyEnterprisesDir(
|
||||
return { copied, deletedLegacyDir };
|
||||
}
|
||||
|
||||
export type CorpConfigLoadIssue = { dir: string; file: string; reason: string };
|
||||
|
||||
let lastCorpLoadIssues: CorpConfigLoadIssue[] = [];
|
||||
// Files already warned about this process: loadCorpConfigs runs several times
|
||||
// per invocation (selection, dispatch, tool handlers), so warn each file once.
|
||||
const warnedCorpFiles = new Set<string>();
|
||||
|
||||
/**
|
||||
* Issues from the most recent loadCorpConfigs() call. A malformed file must not
|
||||
* abort the directory: the remaining enterprises stay usable and the caller
|
||||
* reports the bad file by name instead of failing every command with a bare
|
||||
* JSON parse error.
|
||||
*/
|
||||
export function getLastCorpConfigLoadIssues(): CorpConfigLoadIssue[] {
|
||||
return lastCorpLoadIssues;
|
||||
}
|
||||
|
||||
export function loadCorpConfigs(): CorpConfig[] {
|
||||
const dirs = resolveEnterprisesDirs();
|
||||
const byId = new Map<string, CorpConfig>();
|
||||
const issues: CorpConfigLoadIssue[] = [];
|
||||
|
||||
for (const dir of dirs) {
|
||||
for (const file of readdirSync(dir)
|
||||
.filter((item) => item.endsWith(".json"))
|
||||
.filter((item) => !item.startsWith("example."))) {
|
||||
const content = readFileSync(join(dir, file), "utf-8");
|
||||
const corp = CorpConfigSchema.parse(JSON.parse(content));
|
||||
const fullPath = join(dir, file);
|
||||
let corp: CorpConfig;
|
||||
try {
|
||||
const content = readFileSync(fullPath, "utf-8");
|
||||
corp = CorpConfigSchema.parse(JSON.parse(content));
|
||||
} catch (error) {
|
||||
issues.push({ dir, file, reason: error instanceof Error ? error.message : String(error) });
|
||||
continue;
|
||||
}
|
||||
byId.set(corp.id, corp);
|
||||
}
|
||||
}
|
||||
|
||||
lastCorpLoadIssues = issues;
|
||||
const fresh = issues.filter((issue) => {
|
||||
const key = `${issue.dir}/${issue.file}`;
|
||||
if (warnedCorpFiles.has(key)) return false;
|
||||
warnedCorpFiles.add(key);
|
||||
return true;
|
||||
});
|
||||
if (fresh.length) {
|
||||
console.error(
|
||||
`⚠️ 跳过 ${fresh.length} 个无法解析的企业配置: ${fresh.map((item) => `${item.dir}/${item.file}`).join(", ")}`
|
||||
);
|
||||
console.error(`👉 Next action: seal-use doctor (检查配置文件格式与权限)`);
|
||||
}
|
||||
|
||||
return Array.from(byId.values());
|
||||
}
|
||||
|
||||
@@ -166,13 +205,36 @@ export function resolveGlobalConfigFile(): string {
|
||||
return PRIMARY_GLOBAL_CONFIG_FILE;
|
||||
}
|
||||
|
||||
let lastGlobalConfigIssue: { file: string; reason: string } | undefined;
|
||||
|
||||
/**
|
||||
* Issue from the most recent loadGlobalConfig() call, if the configured file
|
||||
* existed but could not be parsed. `doctor` reads this to surface the bad file;
|
||||
* commands otherwise continue on defaults instead of failing outright.
|
||||
*/
|
||||
export function getLastGlobalConfigIssue(): { file: string; reason: string } | undefined {
|
||||
return lastGlobalConfigIssue;
|
||||
}
|
||||
|
||||
export function loadGlobalConfig(): GlobalConfig {
|
||||
const file = resolveGlobalConfigFile();
|
||||
lastGlobalConfigIssue = undefined;
|
||||
if (!existsSync(file)) {
|
||||
return GlobalConfigSchema.parse({});
|
||||
}
|
||||
|
||||
return GlobalConfigSchema.parse(JSON.parse(readFileSync(file, "utf-8")));
|
||||
try {
|
||||
return GlobalConfigSchema.parse(JSON.parse(readFileSync(file, "utf-8")));
|
||||
} catch (error) {
|
||||
// A hand-edited config.json must not brick every command: fall back to
|
||||
// defaults so the user can still run `seal-use config paths` / `corps list`
|
||||
// to find and fix the file.
|
||||
const reason = error instanceof Error ? error.message : String(error);
|
||||
lastGlobalConfigIssue = { file, reason };
|
||||
console.error(`⚠️ 全局配置无法解析,已回退默认值: ${file} (${reason})`);
|
||||
console.error(`👉 Next action: seal-use config paths (检查配置文件)`);
|
||||
return GlobalConfigSchema.parse({});
|
||||
}
|
||||
}
|
||||
|
||||
export function saveGlobalConfig(config: GlobalConfigInput): GlobalConfig {
|
||||
|
||||
Reference in new issue
Block a user