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
@@ -4,6 +4,7 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
findCorpConfigFile,
|
||||
getLastCorpConfigLoadIssues,
|
||||
loadCorpConfigs,
|
||||
loadGlobalConfig,
|
||||
resolveEnterprisesDir,
|
||||
@@ -186,6 +187,53 @@ describe("loadCorpConfigs", () => {
|
||||
const reloadedAfterClear = loadCorpConfigs().find((c) => c.id === "alpha");
|
||||
expect(reloadedAfterClear?.testCorpId).toBeUndefined();
|
||||
});
|
||||
|
||||
test("skips a malformed file without dropping the other enterprises in the directory", () => {
|
||||
const dir = makeTempDir();
|
||||
process.env.SEAL_USE_ENTERPRISES_DIR = dir;
|
||||
writeCorpConfig(join(dir, "good.json"), "good", "Good Corp");
|
||||
// A hand-edited or truncated file must not abort the whole directory load.
|
||||
writeFileSync(join(dir, "broken.json"), "{ not json at all");
|
||||
writeFileSync(join(dir, "wrong-shape.json"), JSON.stringify({ id: "wrong-shape" }));
|
||||
|
||||
expect(loadCorpConfigs().map((corp) => corp.id)).toEqual(["good"]);
|
||||
|
||||
const issues = getLastCorpConfigLoadIssues();
|
||||
expect(issues.map((issue) => issue.file).sort()).toEqual(["broken.json", "wrong-shape.json"]);
|
||||
expect(issues.map((issue) => issue.dir)).toEqual([dir, dir]);
|
||||
});
|
||||
|
||||
test("reports no issues when every file parses", () => {
|
||||
const dir = makeTempDir();
|
||||
process.env.SEAL_USE_ENTERPRISES_DIR = dir;
|
||||
writeCorpConfig(join(dir, "alpha.json"), "alpha", "Alpha Corp");
|
||||
|
||||
expect(loadCorpConfigs()).toHaveLength(1);
|
||||
expect(getLastCorpConfigLoadIssues()).toEqual([]);
|
||||
});
|
||||
|
||||
test("warns about a malformed file only once per process", () => {
|
||||
const dir = makeTempDir();
|
||||
process.env.SEAL_USE_ENTERPRISES_DIR = dir;
|
||||
writeFileSync(join(dir, "broken-once.json"), "{ nope");
|
||||
|
||||
const errors: string[] = [];
|
||||
const originalError = console.error;
|
||||
console.error = (...values: unknown[]) => {
|
||||
errors.push(values.map((value) => String(value)).join(" "));
|
||||
};
|
||||
try {
|
||||
loadCorpConfigs();
|
||||
loadCorpConfigs();
|
||||
loadCorpConfigs();
|
||||
} finally {
|
||||
console.error = originalError;
|
||||
}
|
||||
|
||||
const skipWarnings = errors.filter((line) => line.includes("跳过"));
|
||||
expect(skipWarnings).toHaveLength(1);
|
||||
expect(skipWarnings[0]).toContain("broken-once.json");
|
||||
});
|
||||
});
|
||||
|
||||
describe("syncLegacyEnterprisesDir", () => {
|
||||
|
||||
Reference in new issue
Block a user