Fix horário calendar save: surface real error, validate sala
The save AJAX endpoint swallowed DB exceptions behind a generic "Erro ao guardar" with no logging, making failures undiagnosable. Now log the real exception (same sink as the global handler) and echo the detail back in dev. Also validate `local` against the allowed sala set instead of only its length, so an out-of-enum value returns a clean 422 instead of a 500 (horario.local is a strict ENUM). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
d5ea2b963c
commit
03102cf1a9
1 file changed
+10
-2
@@ -56,7 +56,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && in_array($_POST['action'] ?? '', ['
|
||||
if ($id < 0 || !array_key_exists($dia, $dias)) $fail(422, 'Dados inválidos');
|
||||
if (!preg_match($hhmm, $hi) || !preg_match($hhmm, $hf)) $fail(422, 'Horas inválidas');
|
||||
if ($hf <= $hi) $fail(422, 'A hora de fim deve ser depois do início');
|
||||
if (mb_strlen($local) > 50) $fail(422, 'Sala inválida');
|
||||
if (!array_key_exists($local, $salas)) $fail(422, 'Sala inválida'); // enum membership
|
||||
if ($mod < 1 || !db_modalidades_get($pdo, $mod)) $fail(422, 'Modalidade inválida');
|
||||
$payload = [
|
||||
'id_modalidade' => $mod,
|
||||
@@ -71,8 +71,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && in_array($_POST['action'] ?? '', ['
|
||||
break;
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
// Surface the real cause: log it (same sink as the global handler) so a
|
||||
// failed save on prod is diagnosable, and echo the detail back in dev.
|
||||
if (function_exists('_error_log_write')) {
|
||||
_error_log_write(get_class($e), $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
|
||||
} else {
|
||||
error_log('[horario/calendar save] ' . $e->getMessage());
|
||||
}
|
||||
http_response_code(500);
|
||||
echo json_encode(['ok' => false, 'error' => 'Erro ao guardar']);
|
||||
$detail = (defined('APP_ENV') && APP_ENV === 'prod') ? 'Erro ao guardar' : $e->getMessage();
|
||||
echo json_encode(['ok' => false, 'error' => $detail]);
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user