From b31030c347d50b78429f665e9100e11753c1acec Mon Sep 17 00:00:00 2001 From: Hosteroid Date: Tue, 14 Oct 2025 01:56:06 +0300 Subject: [PATCH] Improve error handling in import progress fetch Enhances the fetch logic to detect and handle non-JSON server responses, providing clearer error messages for PHP errors or session issues. Also adds specific handling for gateway timeouts by retrying the request after 5 seconds. --- app/Views/tld-registry/import-progress.php | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/app/Views/tld-registry/import-progress.php b/app/Views/tld-registry/import-progress.php index 40fff23..d2b47fb 100644 --- a/app/Views/tld-registry/import-progress.php +++ b/app/Views/tld-registry/import-progress.php @@ -223,7 +223,19 @@ function checkProgress() { } fetch(`/tld-registry/api/import-progress?log_id=${logId}`) - .then(response => response.json()) + .then(response => { + // Check if response is actually JSON + const contentType = response.headers.get('content-type'); + if (!contentType || !contentType.includes('application/json')) { + return response.text().then(text => { + addLogMessage('Server returned non-JSON response. This might be a PHP error or session issue.', 'error'); + addLogMessage('Response preview: ' + text.substring(0, 200) + '...', 'error'); + isComplete = true; + throw new Error('Non-JSON response received'); + }); + } + return response.json(); + }) .then(data => { if (data.error) { addLogMessage('Error: ' + data.error, 'error'); @@ -238,8 +250,13 @@ function checkProgress() { } }) .catch(error => { - addLogMessage('Network error: ' + error.message, 'error'); - isComplete = true; + if (error.message.includes('Gateway Timeout') || error.message.includes('timeout')) { + addLogMessage('Gateway timeout detected. Retrying in 5 seconds...', 'warning'); + setTimeout(checkProgress, 5000); // Retry after 5 seconds + } else { + addLogMessage('Network error: ' + error.message, 'error'); + isComplete = true; + } }); }