Public: improve comment submit error diagnostics and fallback
This commit is contained in:
parent
2760e46a17
commit
ec64282d8a
27
index.php
27
index.php
|
|
@ -37,8 +37,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && (string)($_POST['action'] ?? '') ==
|
||||||
try {
|
try {
|
||||||
$savedComment = commentAdd($photoId, (int)$u['id'], limitText($text, 1000));
|
$savedComment = commentAdd($photoId, (int)$u['id'], limitText($text, 1000));
|
||||||
$commentSaved = true;
|
$commentSaved = true;
|
||||||
} catch (Throwable) {
|
} catch (Throwable $e) {
|
||||||
$errorMessage = 'Не удалось отправить комментарий.';
|
error_log('Comment add failed: ' . $e->getMessage());
|
||||||
|
$errorMessage = 'Ошибка отправки комментария: ' . $e->getMessage();
|
||||||
$errorCode = 500;
|
$errorCode = 500;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1004,9 +1005,25 @@ function outputWatermarked(string $path, string $mime): never
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const payload = await response.json().catch(() => null);
|
const raw = await response.text();
|
||||||
if (!response.ok || !payload || payload.ok !== true) {
|
let payload = null;
|
||||||
throw new Error(payload && payload.message ? String(payload.message) : 'Не удалось отправить комментарий.');
|
try {
|
||||||
|
payload = JSON.parse(raw);
|
||||||
|
} catch {
|
||||||
|
payload = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!payload) {
|
||||||
|
if (response.ok) {
|
||||||
|
window.location.reload();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(raw.trim() !== '' ? raw.slice(0, 220) : 'Не удалось отправить комментарий.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok || payload.ok !== true) {
|
||||||
|
throw new Error(payload.message ? String(payload.message) : 'Не удалось отправить комментарий.');
|
||||||
}
|
}
|
||||||
|
|
||||||
setCommentFeedback(payload.message || 'Ваш комментарий отправлен.', false);
|
setCommentFeedback(payload.message || 'Ваш комментарий отправлен.', false);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user