logs & exceptions

This commit is contained in:
Eric Espie
2026-06-09 08:56:05 +02:00
parent 7d0005acf3
commit 806a5b92df
2 changed files with 43 additions and 35 deletions

View File

@@ -4,11 +4,12 @@ namespace Combodo\iTop\Application\Helper;
use Combodo\iTop\Application\WebPage\ErrorPage;
use IssueLog;
use SimpleXMLElement;
use Throwable;
class ExceptionHandlerHelper
{
static array $aSupportedMimeTypes = [
public static array $aSupportedMimeTypes = [
'application/json' => 'json',
'application/xml' => 'xml',
'text/html' => 'html',
@@ -17,7 +18,10 @@ class ExceptionHandlerHelper
public static function HandleException(Throwable $oException)
{
ob_end_clean();
$aStatus = ob_get_status();
if (count($aStatus) !== 0) {
ob_end_clean();
}
// Log the exception
IssueLog::Exception('Fatal error', $oException);
@@ -36,18 +40,23 @@ class ExceptionHandlerHelper
header("Content-Type: {$mime}; charset=utf-8");
header('Vary: Accept');
$aData = [
'error' => 'Fatal error',
'message' => 'We are sorry, an unexpected error has occurred. Please try again later.',
];
switch (self::$aSupportedMimeTypes[$mime]) {
case 'json':
// echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
echo json_encode($aData, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
break;
case 'xml':
// Version simple: sérialisation minimale
// $xml = new SimpleXMLElement('<response/>');
// array_walk_recursive((array)$data, function ($value, $key) use ($xml) {
// $xml->addChild((string)$key, htmlspecialchars((string)$value, ENT_QUOTES | ENT_XML1, 'UTF-8'));
// });
// echo $xml->asXML();
$oXml = new SimpleXMLElement('<response/>');
array_walk_recursive($aData, function ($sValue, $sKey) use ($oXml) {
$oXml->addChild((string)$sKey, htmlspecialchars((string)$sValue, ENT_QUOTES | ENT_XML1, 'UTF-8'));
});
echo $oXml->asXML();
break;
case 'html':
@@ -58,9 +67,9 @@ class ExceptionHandlerHelper
break;
case 'text':
echo 'Fatal error';
echo "Fatal error\n";
echo "We are sorry, an unexpected error has occurred. Please try again later.\n";
break;
}
}
@@ -88,7 +97,7 @@ class ExceptionHandlerHelper
$accepted[] = ['type' => $type, 'q' => $q];
}
usort($accepted, fn($a, $b) => $b['q'] <=> $a['q']);
usort($accepted, fn ($a, $b) => $b['q'] <=> $a['q']);
foreach ($accepted as $a) {
foreach ($supportedMimes as $mime) {
@@ -107,4 +116,4 @@ class ExceptionHandlerHelper
return null;
}
}
}

View File

@@ -23,7 +23,6 @@ namespace Combodo\iTop\Application\TwigBase\Controller;
use ApplicationMenu;
use Combodo\iTop\Application\TwigBase\Twig\TwigHelper;
use Combodo\iTop\Application\WebPage\AjaxPage;
use Combodo\iTop\Application\WebPage\ErrorPage;
use Combodo\iTop\Application\WebPage\iTopWebPage;
use Combodo\iTop\Application\WebPage\WebPage;
use Combodo\iTop\Controller\AbstractController;
@@ -261,29 +260,29 @@ abstract class Controller extends AbstractController
*/
public function HandleOperation(): void
{
try {
$this->CheckAccess();
$this->m_sOperation = utils::ReadParam('operation', $this->sDefaultOperation);
// try {
$this->CheckAccess();
$this->m_sOperation = utils::ReadParam('operation', $this->sDefaultOperation);
if ($this->CallOperation(utils::ToCamelCase($this->m_sOperation))) {
return;
}
// Fallback to unchanged names for compatibility
if ($this->CallOperation($this->m_sOperation)) {
return;
}
$this->DisplayBadRequest();
} catch (Exception $e) {
http_response_code(500);
$oP = new ErrorPage(Dict::S('UI:PageTitle:FatalError'));
$oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
$oP->add(get_class($e).' : '.utils::EscapeHtml($e->GetMessage()));
$oP->output();
IssueLog::Exception('HandleOperation failed for '.json_encode($this->m_sOperation), $e);
if ($this->CallOperation(utils::ToCamelCase($this->m_sOperation))) {
return;
}
// Fallback to unchanged names for compatibility
if ($this->CallOperation($this->m_sOperation)) {
return;
}
$this->DisplayBadRequest();
// } catch (Exception $e) {
// http_response_code(500);
// $oP = new ErrorPage(Dict::S('UI:PageTitle:FatalError'));
// $oP->add("<h1>".Dict::S('UI:FatalErrorMessage')."</h1>\n");
// $oP->add(get_class($e).' : '.utils::EscapeHtml($e->GetMessage()));
// $oP->output();
//
// IssueLog::Exception('HandleOperation failed for '.json_encode($this->m_sOperation), $e);
// }
}
/**