From 806a5b92dff52f7575868bf5265b3c016d58a189 Mon Sep 17 00:00:00 2001 From: Eric Espie Date: Tue, 9 Jun 2026 08:56:05 +0200 Subject: [PATCH] logs & exceptions --- .../Helper/ExceptionHandlerHelper.php | 35 +++++++++------ .../TwigBase/Controller/Controller.php | 43 +++++++++---------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/sources/Application/Helper/ExceptionHandlerHelper.php b/sources/Application/Helper/ExceptionHandlerHelper.php index af83f241c..af28fa9fb 100644 --- a/sources/Application/Helper/ExceptionHandlerHelper.php +++ b/sources/Application/Helper/ExceptionHandlerHelper.php @@ -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(''); -// 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(''); + 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; } -} \ No newline at end of file +} diff --git a/sources/Application/TwigBase/Controller/Controller.php b/sources/Application/TwigBase/Controller/Controller.php index d60e6e518..ee3689f3c 100644 --- a/sources/Application/TwigBase/Controller/Controller.php +++ b/sources/Application/TwigBase/Controller/Controller.php @@ -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("

".Dict::S('UI:FatalErrorMessage')."

\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("

".Dict::S('UI:FatalErrorMessage')."

\n"); + // $oP->add(get_class($e).' : '.utils::EscapeHtml($e->GetMessage())); + // $oP->output(); + // + // IssueLog::Exception('HandleOperation failed for '.json_encode($this->m_sOperation), $e); + // } } /**