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:
andreandClaude Opus 4.8 committed 2026-06-16 12:16:11 +01:00
1 parent d5ea2b963c
commit 03102cf1a9
1 file changed
+10 -2
+10 -2
View File
@@ -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 ($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 (!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 ($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'); if ($mod < 1 || !db_modalidades_get($pdo, $mod)) $fail(422, 'Modalidade inválida');
$payload = [ $payload = [
'id_modalidade' => $mod, 'id_modalidade' => $mod,
@@ -71,8 +71,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && in_array($_POST['action'] ?? '', ['
break; break;
} }
} catch (Throwable $e) { } 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); 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; exit;
} }