
















@@ -127,6 +127,8 @@ function createConfigObserveAuditRecord(params: {
127127clobberedPath: string | null;
128128restoredFromBackup: boolean;
129129restoredBackupPath: string | null;
130+restoreErrorCode?: string | null;
131+restoreErrorMessage?: string | null;
130132}): ConfigObserveAuditRecord {
131133return {
132134ts: params.ts,
@@ -175,6 +177,8 @@ function createConfigObserveAuditRecord(params: {
175177clobberedPath: params.clobberedPath,
176178restoredFromBackup: params.restoredFromBackup,
177179restoredBackupPath: params.restoredBackupPath,
180+restoreErrorCode: params.restoreErrorCode ?? null,
181+restoreErrorMessage: params.restoreErrorMessage ?? null,
178182};
179183}
180184@@ -192,6 +196,24 @@ function createConfigObserveAuditAppendParams(
192196};
193197}
194198199+function extractRestoreErrorDetails(error: unknown): {
200+code: string | null;
201+message: string | null;
202+} {
203+if (!error || typeof error !== "object") {
204+return { code: null, message: typeof error === "string" ? error : null };
205+}
206+const code =
207+"code" in error && typeof (error as { code?: unknown }).code === "string"
208+ ? (error as { code: string }).code
209+ : null;
210+const message =
211+"message" in error && typeof (error as { message?: unknown }).message === "string"
212+ ? (error as { message: string }).message
213+ : null;
214+return { code, message };
215+}
216+195217function hashConfigRaw(raw: string | null): string {
196218return crypto
197219.createHash("sha256")
@@ -637,26 +659,43 @@ export async function maybeRecoverSuspiciousConfigRead(params: {
637659});
638660639661let restoredFromBackup = false;
662+let restoreError: unknown;
640663try {
641664await params.deps.fs.promises.copyFile(backupPath, params.configPath);
642665restoredFromBackup = true;
643-} catch {}
666+} catch (error) {
667+restoreError = error;
668+}
644669645-params.deps.logger.warn(
646-`Config auto-restored from backup: ${params.configPath} (${suspicious.join(", ")})`,
647-);
670+const restoreErrorDetails = restoredFromBackup
671+ ? { code: null, message: null }
672+ : extractRestoreErrorDetails(restoreError);
673+674+if (restoredFromBackup) {
675+params.deps.logger.warn(
676+`Config auto-restored from backup: ${params.configPath} (${suspicious.join(", ")})`,
677+);
678+} else {
679+params.deps.logger.warn(
680+`Config auto-restore from backup failed: ${params.configPath} (${suspicious.join(", ")}${
681+ restoreErrorDetails.message ? `; ${restoreErrorDetails.message}` : ""
682+ })`,
683+);
684+}
648685await appendConfigAuditRecord(
649686createConfigObserveAuditAppendParams(params.deps, {
650687ts: now,
651688configPath: params.configPath,
652-valid: true,
689+valid: restoredFromBackup,
653690 current,
654691 suspicious,
655692lastKnownGood: entry.lastKnownGood,
656693 backup,
657694 clobberedPath,
658695 restoredFromBackup,
659696restoredBackupPath: backupPath,
697+restoreErrorCode: restoreErrorDetails.code,
698+restoreErrorMessage: restoreErrorDetails.message,
660699}),
661700);
662701@@ -727,26 +766,43 @@ export function maybeRecoverSuspiciousConfigReadSync(params: {
727766});
728767729768let restoredFromBackup = false;
769+let restoreError: unknown;
730770try {
731771params.deps.fs.copyFileSync(backupPath, params.configPath);
732772restoredFromBackup = true;
733-} catch {}
773+} catch (error) {
774+restoreError = error;
775+}
734776735-params.deps.logger.warn(
736-`Config auto-restored from backup: ${params.configPath} (${suspicious.join(", ")})`,
737-);
777+const restoreErrorDetails = restoredFromBackup
778+ ? { code: null, message: null }
779+ : extractRestoreErrorDetails(restoreError);
780+781+if (restoredFromBackup) {
782+params.deps.logger.warn(
783+`Config auto-restored from backup: ${params.configPath} (${suspicious.join(", ")})`,
784+);
785+} else {
786+params.deps.logger.warn(
787+`Config auto-restore from backup failed: ${params.configPath} (${suspicious.join(", ")}${
788+ restoreErrorDetails.message ? `; ${restoreErrorDetails.message}` : ""
789+ })`,
790+);
791+}
738792appendConfigAuditRecordSync(
739793createConfigObserveAuditAppendParams(params.deps, {
740794ts: now,
741795configPath: params.configPath,
742-valid: true,
796+valid: restoredFromBackup,
743797 current,
744798 suspicious,
745799lastKnownGood: entry.lastKnownGood,
746800 backup,
747801 clobberedPath,
748802 restoredFromBackup,
749803restoredBackupPath: backupPath,
804+restoreErrorCode: restoreErrorDetails.code,
805+restoreErrorMessage: restoreErrorDetails.message,
750806}),
751807);
752808此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。