diff --git a/app/templates/modules/settings/database_management.html b/app/templates/modules/settings/database_management.html index f97e6bd..488713a 100755 --- a/app/templates/modules/settings/database_management.html +++ b/app/templates/modules/settings/database_management.html @@ -477,7 +477,10 @@ document.addEventListener('DOMContentLoaded', function() { 'Content-Type': 'application/json' } }) - .then(response => response.json()) + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }) .then(data => { if (data.success) { alert('Cleanup completed! ' + (data.deleted_count || 0) + ' old backups deleted.'); @@ -512,7 +515,10 @@ document.addEventListener('DOMContentLoaded', function() { method: 'POST', body: formData }) - .then(response => response.json()) + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }) .then(data => { if (data.success) { alert('Backup created successfully: ' + data.file); @@ -677,7 +683,15 @@ document.addEventListener('DOMContentLoaded', function() { }, body: JSON.stringify({ table: table }) }) - .then(response => response.json()) + .then(response => { + if (!response.ok) { + if (response.status === 401) { + throw new Error('Unauthorized. Please log in again.'); + } + throw new Error(`HTTP error! status: ${response.status}`); + } + return response.json(); + }) .then(data => { if (data.success) { // Hide modal @@ -701,16 +715,16 @@ document.addEventListener('DOMContentLoaded', function() { } else { alert('Error: ' + data.error); // Re-enable button - this.disabled = false; - this.innerHTML = ' Yes, Clear All Data'; + confirmBtn.disabled = false; + confirmBtn.innerHTML = ' Yes, Clear All Data'; } }) .catch(error => { console.error('Error:', error); - alert('Error clearing table: ' + error); + alert('Error clearing table: ' + error.message); // Re-enable button - this.disabled = false; - this.innerHTML = ' Yes, Clear All Data'; + confirmBtn.disabled = false; + confirmBtn.innerHTML = ' Yes, Clear All Data'; }); }); } @@ -739,7 +753,10 @@ document.addEventListener('DOMContentLoaded', function() { }, body: JSON.stringify({ backup: backup }) }) - .then(response => response.json()) + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }) .then(data => { if (data.success) { alert('Database restored successfully!'); @@ -758,7 +775,10 @@ document.addEventListener('DOMContentLoaded', function() { function loadBackupsList() { fetch('{{ url_for("settings.get_backups_list") }}') - .then(response => response.json()) + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }) .then(data => { if (data.backups && data.backups.length > 0) { const tbody = document.getElementById('backups-list'); @@ -837,7 +857,10 @@ function toggleDayOfWeek() { function loadBackupSchedules() { fetch('{{ url_for("settings.get_backup_schedules") }}') - .then(response => response.json()) + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }) .then(data => { const tbody = document.getElementById('schedules-list'); @@ -925,7 +948,10 @@ function saveBackupSchedule() { }, body: JSON.stringify(payload) }) - .then(response => response.json()) + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }) .then(data => { if (data.success) { alert('Backup schedule created successfully!'); @@ -949,7 +975,10 @@ function deleteSchedule(scheduleId) { 'Content-Type': 'application/json' } }) - .then(response => response.json()) + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }) .then(data => { if (data.success) { alert('Schedule deleted successfully!'); @@ -972,7 +1001,10 @@ function toggleSchedule(scheduleId) { 'Content-Type': 'application/json' } }) - .then(response => response.json()) + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }) .then(data => { if (data.success) { loadBackupSchedules(); @@ -1010,7 +1042,10 @@ function uploadBackupFile() { method: 'POST', body: formData }) - .then(response => response.json()) + .then(response => { + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); + return response.json(); + }) .then(data => { if (data.success) { statusDiv.innerHTML = '