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 (?, ?)
|
VALUES (?, ?)
|
||||||
ON DUPLICATE KEY UPDATE concepto = VALUES(concepto)"
|
ON DUPLICATE KEY UPDATE concepto = VALUES(concepto)"
|
||||||
);
|
);
|
||||||
|
if (!$stmt) {
|
||||||
|
$info = $pdo->errorInfo();
|
||||||
|
throw new \RuntimeException("Prepare falló [{$info[0]}]: {$info[2]}");
|
||||||
|
}
|
||||||
|
|
||||||
$insertados = 0;
|
$insertados = 0;
|
||||||
foreach ($data['rows'] as $row) {
|
foreach ($data['rows'] as $row) {
|
||||||
$cod = trim($row['cod'] ?? '');
|
$cod = trim($row['cod'] ?? '');
|
||||||
$concepto = trim($row['concepto'] ?? '');
|
$concepto = trim($row['concepto'] ?? '');
|
||||||
if (!$cod || !$concepto) continue;
|
if (!$cod || !$concepto) continue;
|
||||||
$stmt->execute([$cod, $concepto]);
|
$stmt->execute([$cod, $concepto]);
|
||||||
@@ -55,7 +59,7 @@ try {
|
|||||||
|
|
||||||
ob_clean();
|
ob_clean();
|
||||||
echo json_encode(['ok' => true, 'insertados' => $insertados]);
|
echo json_encode(['ok' => true, 'insertados' => $insertados]);
|
||||||
} catch (Exception $e) {
|
} catch (\Throwable $e) {
|
||||||
ob_clean();
|
ob_clean();
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
echo json_encode(['ok' => false, 'error' => $e->getMessage()]);
|
echo json_encode(['ok' => false, 'error' => $e->getMessage()]);
|
||||||
|
|||||||
+59
-13
@@ -132,6 +132,43 @@ if (!in_array($modo, ['insertar', 'upsert'], true)) {
|
|||||||
$modo = 'upsert';
|
$modo = 'upsert';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Exámenes opcionales (vienen del scheduler de RIPS) ───────────────────────
|
||||||
|
$examenesRaw = $data['examenes'] ?? null;
|
||||||
|
$examenesGuardados = 0;
|
||||||
|
|
||||||
|
function guardarExamenesPendientes(string $doc, array $examenes): int {
|
||||||
|
if (!$examenes || !$doc) return 0;
|
||||||
|
$pdo = db();
|
||||||
|
|
||||||
|
$stDel = $pdo->prepare(
|
||||||
|
"DELETE FROM rips_examenes_pendientes
|
||||||
|
WHERE numero_documento = ? AND DATE(created_at) = CURDATE()"
|
||||||
|
);
|
||||||
|
if (!$stDel) {
|
||||||
|
$info = $pdo->errorInfo();
|
||||||
|
throw new \RuntimeException("rips_examenes_pendientes no existe o error: {$info[2]}");
|
||||||
|
}
|
||||||
|
$stDel->execute([$doc]);
|
||||||
|
|
||||||
|
$meta = $examenes[0] ?? [];
|
||||||
|
$stIns = $pdo->prepare(
|
||||||
|
"INSERT INTO rips_examenes_pendientes
|
||||||
|
(numero_documento, datos, recepcion_id, hora_recepcion)
|
||||||
|
VALUES (?, ?, ?, ?)"
|
||||||
|
);
|
||||||
|
if (!$stIns) {
|
||||||
|
$info = $pdo->errorInfo();
|
||||||
|
throw new \RuntimeException("INSERT rips_examenes_pendientes falló: {$info[2]}");
|
||||||
|
}
|
||||||
|
$stIns->execute([
|
||||||
|
$doc,
|
||||||
|
json_encode($examenes, JSON_UNESCAPED_UNICODE),
|
||||||
|
$meta['recepcion_id'] ?? null,
|
||||||
|
$meta['hora'] ?? null,
|
||||||
|
]);
|
||||||
|
return count($examenes);
|
||||||
|
}
|
||||||
|
|
||||||
// ── UPSERT / INSERT-ONLY ─────────────────────────────────────────────────────
|
// ── UPSERT / INSERT-ONLY ─────────────────────────────────────────────────────
|
||||||
try {
|
try {
|
||||||
$pac = new Paciente();
|
$pac = new Paciente();
|
||||||
@@ -144,31 +181,40 @@ try {
|
|||||||
ob_clean();
|
ob_clean();
|
||||||
|
|
||||||
if ($existente) {
|
if ($existente) {
|
||||||
|
if (!empty($examenesRaw) && is_array($examenesRaw)) {
|
||||||
|
$examenesGuardados = guardarExamenesPendientes($campos['numero_documento'], $examenesRaw);
|
||||||
|
}
|
||||||
if ($modo === 'insertar') {
|
if ($modo === 'insertar') {
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'ok' => true,
|
'ok' => true,
|
||||||
'action' => 'skipped',
|
'action' => 'skipped',
|
||||||
'id' => $existente['id'],
|
'id' => $existente['id'],
|
||||||
'message' => 'Paciente ya existe',
|
'message' => 'Paciente ya existe',
|
||||||
|
'examenes_guardados' => $examenesGuardados,
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
// Nunca pisar origen en update — conservar el que ya tiene en BD
|
// Nunca pisar origen en update — conservar el que ya tiene en BD
|
||||||
unset($campos['origen']);
|
unset($campos['origen']);
|
||||||
$pac->actualizar($existente['id'], $campos, null);
|
$pac->actualizar($existente['id'], $campos, null);
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'ok' => true,
|
'ok' => true,
|
||||||
'action' => 'updated',
|
'action' => 'updated',
|
||||||
'id' => $existente['id'],
|
'id' => $existente['id'],
|
||||||
'message' => 'Paciente actualizado',
|
'message' => 'Paciente actualizado',
|
||||||
|
'examenes_guardados' => $examenesGuardados,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
} elseif (!empty($campos['nombre_completo'])) {
|
} elseif (!empty($campos['nombre_completo'])) {
|
||||||
$id = $pac->crear($campos, null);
|
$id = $pac->crear($campos, null);
|
||||||
|
if (!empty($examenesRaw) && is_array($examenesRaw)) {
|
||||||
|
$examenesGuardados = guardarExamenesPendientes($campos['numero_documento'], $examenesRaw);
|
||||||
|
}
|
||||||
echo json_encode([
|
echo json_encode([
|
||||||
'ok' => true,
|
'ok' => true,
|
||||||
'action' => 'created',
|
'action' => 'created',
|
||||||
'id' => $id,
|
'id' => $id,
|
||||||
'message' => 'Paciente creado',
|
'message' => 'Paciente creado',
|
||||||
|
'examenes_guardados' => $examenesGuardados,
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
http_response_code(422);
|
http_response_code(422);
|
||||||
@@ -178,7 +224,7 @@ try {
|
|||||||
'error' => 'Sin número de documento ni nombre: registro omitido',
|
'error' => 'Sin número de documento ni nombre: registro omitido',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
} catch (\Throwable $e) {
|
||||||
ob_clean();
|
ob_clean();
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
echo json_encode(['ok' => false, 'error' => $e->getMessage()]);
|
echo json_encode(['ok' => false, 'error' => $e->getMessage()]);
|
||||||
|
|||||||
Reference in New Issue
Block a user