Wrap ensureDir in try-catch blocks

This commit is contained in:
mikiher 2025-12-01 18:00:34 +02:00
parent 329e3c7179
commit 5f1edcb609
5 changed files with 67 additions and 22 deletions

View file

@ -48,9 +48,34 @@ class BackupManager {
}
async init() {
const backupsDirExists = await fs.pathExists(this.backupPath)
if (!backupsDirExists) {
await fs.ensureDir(this.backupPath)
// Validate backupPath before attempting to create it
if (!this.backupPath || typeof this.backupPath !== 'string' || this.backupPath.length < 2) {
Logger.error(`[BackupManager] Invalid backup path configured: "${this.backupPath}". Falling back to default.`)
// Reset to default backup path
global.ServerSettings.backupPath = Path.join(global.MetadataPath, 'backups')
}
try {
const backupsDirExists = await fs.pathExists(this.backupPath)
if (!backupsDirExists) {
await fs.ensureDir(this.backupPath)
}
} catch (error) {
Logger.error(`[BackupManager] Failed to ensure backup directory at "${this.backupPath}": ${error.message}`)
// Attempt to fall back to default path
const defaultBackupPath = Path.join(global.MetadataPath, 'backups')
if (this.backupPath !== defaultBackupPath) {
Logger.info(`[BackupManager] Attempting to use default backup path: "${defaultBackupPath}"`)
global.ServerSettings.backupPath = defaultBackupPath
try {
await fs.ensureDir(defaultBackupPath)
} catch (fallbackError) {
Logger.error(`[BackupManager] Failed to create default backup directory: ${fallbackError.message}`)
throw new Error(`[BackupManager] Failed to create default backup directory at "${defaultBackupPath}"`, { cause: fallbackError })
}
} else {
throw new Error(`[BackupManager] Failed to ensure backup directory at "${this.backupPath}"`, { cause: error })
}
}
await this.loadBackups()