Skip to content

Commit be2f12f

Browse files
committed
refactor(backup): hoist restore failure handling out of catch scopes
1 parent bf1ae58 commit be2f12f

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

‎src/commands/backup-restore.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,25 +132,33 @@ export async function backupRestoreCommand(
132132
const verified = await verifyBackupArchive(options.archive);
133133
const target = await prepareRestoreTarget(targetPath);
134134

135+
let extractionError: unknown;
136+
let extractionFailed = false;
135137
try {
136138
await extractBackupArchive(verified.archivePath, targetPath);
137-
} catch (error) {
139+
} catch (caughtExtractionError) {
140+
extractionError = caughtExtractionError;
141+
extractionFailed = true;
142+
}
143+
if (extractionFailed) {
138144
let cleanupError: unknown;
145+
let cleanupFailed = false;
139146
try {
140147
await cleanupFailedRestore(targetPath, target.created);
141148
} catch (caughtCleanupError) {
142149
cleanupError = caughtCleanupError;
150+
cleanupFailed = true;
143151
}
144-
if (cleanupError !== undefined) {
152+
if (cleanupFailed) {
145153
// Extraction stays the primary cause; cleanup rides along as the second AggregateError entry.
146154
throw new AggregateError(
147-
[error, cleanupError],
155+
[extractionError, cleanupError],
148156
`Backup restore failed and the incomplete target could not be cleaned: ${targetPath}. Cleanup error: ${formatErrorMessage(cleanupError)}`,
149-
{ cause: error },
157+
{ cause: extractionError },
150158
);
151159
}
152160
throw new Error(`Backup restore failed; the incomplete target was cleaned: ${targetPath}`, {
153-
cause: error,
161+
cause: extractionError,
154162
});
155163
}
156164

0 commit comments

Comments
 (0)