fix: catch Throwable instead of Exception, add prepare() null checks

PHP's PDO in silent mode returns false on prepare() when table doesn't
exist; calling ->execute() on false throws TypeError (Error, not
Exception). Changed catch to \Throwable and added explicit checks so
the real error message is returned as JSON instead of HTTP 500 empty.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-17 10:58:13 -05:00
co-authored by Claude Sonnet 4.6
parent f5de2c90ca
commit 3d0ae95d1e
2 changed files with 65 additions and 15 deletions
+6 -2
View File
@@ -43,10 +43,14 @@ try {
VALUES (?, ?)
ON DUPLICATE KEY UPDATE concepto = VALUES(concepto)"
);
if (!$stmt) {
$info = $pdo->errorInfo();
throw new \RuntimeException("Prepare falló [{$info[0]}]: {$info[2]}");
}
$insertados = 0;
foreach ($data['rows'] as $row) {
$cod = trim($row['cod'] ?? '');
$cod = trim($row['cod'] ?? '');
$concepto = trim($row['concepto'] ?? '');
if (!$cod || !$concepto) continue;
$stmt->execute([$cod, $concepto]);
@@ -55,7 +59,7 @@ try {
ob_clean();
echo json_encode(['ok' => true, 'insertados' => $insertados]);
} catch (Exception $e) {
} catch (\Throwable $e) {
ob_clean();
http_response_code(500);
echo json_encode(['ok' => false, 'error' => $e->getMessage()]);