Implement error handling and JSON response for ajax_inscr_gestion.php

This commit enhances the error handling in the `ajax_inscr_gestion.php` file by introducing a shutdown function that captures fatal errors and returns a structured JSON response instead of a generic 500 error. Additionally, it adds diagnostic information for invalid actions, improving the debugging process. These changes aim to provide clearer feedback to users and developers when issues arise during AJAX requests.
This commit is contained in:
2026-06-29 09:46:05 -04:00
parent 6ab8c8c61f
commit 61bf838108

View File

@ -5,12 +5,30 @@
*/
session_start();
header('Content-Type: application/json; charset=utf-8');
// Filet de securite : transforme tout fatal (include manquant, fonction
// indefinie, parse error...) en reponse JSON exploitable au lieu d'un 500 muet.
$GLOBALS['__inscr_gestion_done'] = false;
register_shutdown_function(function () {
if (!empty($GLOBALS['__inscr_gestion_done'])) {
return;
}
$err = error_get_last();
$strDetail = ($err && in_array($err['type'], array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR), true))
? ($err['message'] . ' @ ' . $err['file'] . ':' . $err['line'])
: 'fatal inconnu';
error_log('[ajax_inscr_gestion] ' . $strDetail);
if (!headers_sent()) {
http_response_code(200);
}
echo json_encode(array('state' => 'error', 'message' => 'fatal', 'detail' => $strDetail));
});
require_once 'php/inc_fonctions.php';
require_once 'php/inc_fx_promoteur.php';
require_once 'php/inc_fx_inscriptions_gestion.php';
header('Content-Type: application/json; charset=utf-8');
$intComId = intval($_SESSION['com_id'] ?? 0);
$strAction = trim((string)($_REQUEST['a'] ?? ''));
$strLangue = trim((string)($_REQUEST['lang'] ?? 'fr'));
@ -19,12 +37,14 @@ if ($strLangue === '') {
}
if ($intComId <= 0) {
$GLOBALS['__inscr_gestion_done'] = true;
echo json_encode(array('state' => 'error', 'message' => 'session'));
exit;
}
$tabRetour = array('state' => 'error');
try {
switch ($strAction) {
case 'par_statut_course':
$intParId = intval($_REQUEST['par_id'] ?? 0);
@ -110,8 +130,34 @@ switch ($strAction) {
break;
default:
$tabRetour = array('state' => 'error', 'message' => 'invalid_action');
// DIAGNOSTIC TEMPORAIRE : expose ce que le serveur a reellement recu
// pour comprendre pourquoi l'action ne matche aucun case.
$tabRetour = array(
'state' => 'error',
'message' => 'invalid_action',
'debug' => array(
'a_recu' => $strAction,
'a_len' => strlen($strAction),
'request_keys' => array_keys($_REQUEST),
'post_keys' => array_keys($_POST),
'get_keys' => array_keys($_GET),
'method' => $_SERVER['REQUEST_METHOD'] ?? '',
'content_type' => $_SERVER['CONTENT_TYPE'] ?? '',
),
);
error_log('[ajax_inscr_gestion] invalid_action a=' . json_encode($strAction)
. ' REQUEST_keys=' . json_encode(array_keys($_REQUEST))
. ' POST_keys=' . json_encode(array_keys($_POST)));
break;
}
} catch (\Throwable $e) {
error_log('[ajax_inscr_gestion] ' . $e->getMessage() . ' @ ' . $e->getFile() . ':' . $e->getLine());
$tabRetour = array(
'state' => 'error',
'message' => 'exception',
'detail' => $e->getMessage() . ' @ ' . $e->getFile() . ':' . $e->getLine(),
);
}
$GLOBALS['__inscr_gestion_done'] = true;
echo json_encode($tabRetour);