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:
co-authored by
Claude Sonnet 4.6
parent
f5de2c90ca
commit
3d0ae95d1e
@@ -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()]);
|
||||
|
||||
Reference in New Issue
Block a user