Skip to content

Commit 915df34

Browse files
committed
N°8781 - Improve twig base controller render error report
1 parent f2da276 commit 915df34

File tree

5 files changed

+64
-28
lines changed

5 files changed

+64
-28
lines changed

lib/composer/autoload_classmap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144
'Combodo\\iTop\\Application\\Search\\CriterionParser' => $baseDir . '/sources/Application/Search/criterionparser.class.inc.php',
145145
'Combodo\\iTop\\Application\\Search\\SearchForm' => $baseDir . '/sources/Application/Search/searchform.class.inc.php',
146146
'Combodo\\iTop\\Application\\Status\\Status' => $baseDir . '/sources/Application/Status/Status.php',
147+
'Combodo\\iTop\\Application\\Symfony\\Poc\\BaseForm\\BaseFormController' => $baseDir . '/sources/Application/Symfony/Poc/BaseForm/BaseFormController.php',
148+
'Combodo\\iTop\\Application\\Symfony\\Poc\\BaseForm\\BaseFormType' => $baseDir . '/sources/Application/Symfony/Poc/BaseForm/BaseFormType.php',
147149
'Combodo\\iTop\\Application\\TwigBase\\Controller\\Controller' => $baseDir . '/sources/Application/TwigBase/Controller/Controller.php',
148150
'Combodo\\iTop\\Application\\TwigBase\\Controller\\PageNotFoundException' => $baseDir . '/application/exceptions/PageNotFoundException.php',
149151
'Combodo\\iTop\\Application\\TwigBase\\Twig\\Extension' => $baseDir . '/sources/Application/TwigBase/Twig/Extension.php',
@@ -3045,7 +3047,6 @@
30453047
'Symfony\\Component\\Security\\Core\\Signature\\Exception\\InvalidSignatureException' => $vendorDir . '/symfony/security-core/Signature/Exception/InvalidSignatureException.php',
30463048
'Symfony\\Component\\Security\\Core\\Signature\\ExpiredSignatureStorage' => $vendorDir . '/symfony/security-core/Signature/ExpiredSignatureStorage.php',
30473049
'Symfony\\Component\\Security\\Core\\Signature\\SignatureHasher' => $vendorDir . '/symfony/security-core/Signature/SignatureHasher.php',
3048-
'Symfony\\Component\\Security\\Core\\Test\\AccessDecisionStrategyTestCase' => $vendorDir . '/symfony/security-core/Test/AccessDecisionStrategyTestCase.php',
30493050
'Symfony\\Component\\Security\\Core\\User\\AttributesBasedUserProviderInterface' => $vendorDir . '/symfony/security-core/User/AttributesBasedUserProviderInterface.php',
30503051
'Symfony\\Component\\Security\\Core\\User\\ChainUserChecker' => $vendorDir . '/symfony/security-core/User/ChainUserChecker.php',
30513052
'Symfony\\Component\\Security\\Core\\User\\ChainUserProvider' => $vendorDir . '/symfony/security-core/User/ChainUserProvider.php',

lib/composer/autoload_static.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,8 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
540540
'Combodo\\iTop\\Application\\Search\\CriterionParser' => __DIR__ . '/../..' . '/sources/Application/Search/criterionparser.class.inc.php',
541541
'Combodo\\iTop\\Application\\Search\\SearchForm' => __DIR__ . '/../..' . '/sources/Application/Search/searchform.class.inc.php',
542542
'Combodo\\iTop\\Application\\Status\\Status' => __DIR__ . '/../..' . '/sources/Application/Status/Status.php',
543+
'Combodo\\iTop\\Application\\Symfony\\Poc\\BaseForm\\BaseFormController' => __DIR__ . '/../..' . '/sources/Application/Symfony/Poc/BaseForm/BaseFormController.php',
544+
'Combodo\\iTop\\Application\\Symfony\\Poc\\BaseForm\\BaseFormType' => __DIR__ . '/../..' . '/sources/Application/Symfony/Poc/BaseForm/BaseFormType.php',
543545
'Combodo\\iTop\\Application\\TwigBase\\Controller\\Controller' => __DIR__ . '/../..' . '/sources/Application/TwigBase/Controller/Controller.php',
544546
'Combodo\\iTop\\Application\\TwigBase\\Controller\\PageNotFoundException' => __DIR__ . '/../..' . '/application/exceptions/PageNotFoundException.php',
545547
'Combodo\\iTop\\Application\\TwigBase\\Twig\\Extension' => __DIR__ . '/../..' . '/sources/Application/TwigBase/Twig/Extension.php',
@@ -3441,7 +3443,6 @@ class ComposerStaticInit7f81b4a2a468a061c306af5e447a9a9f
34413443
'Symfony\\Component\\Security\\Core\\Signature\\Exception\\InvalidSignatureException' => __DIR__ . '/..' . '/symfony/security-core/Signature/Exception/InvalidSignatureException.php',
34423444
'Symfony\\Component\\Security\\Core\\Signature\\ExpiredSignatureStorage' => __DIR__ . '/..' . '/symfony/security-core/Signature/ExpiredSignatureStorage.php',
34433445
'Symfony\\Component\\Security\\Core\\Signature\\SignatureHasher' => __DIR__ . '/..' . '/symfony/security-core/Signature/SignatureHasher.php',
3444-
'Symfony\\Component\\Security\\Core\\Test\\AccessDecisionStrategyTestCase' => __DIR__ . '/..' . '/symfony/security-core/Test/AccessDecisionStrategyTestCase.php',
34453446
'Symfony\\Component\\Security\\Core\\User\\AttributesBasedUserProviderInterface' => __DIR__ . '/..' . '/symfony/security-core/User/AttributesBasedUserProviderInterface.php',
34463447
'Symfony\\Component\\Security\\Core\\User\\ChainUserChecker' => __DIR__ . '/..' . '/symfony/security-core/User/ChainUserChecker.php',
34473448
'Symfony\\Component\\Security\\Core\\User\\ChainUserProvider' => __DIR__ . '/..' . '/symfony/security-core/User/ChainUserProvider.php',

sources/Application/TwigBase/Controller/Controller.php

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
use Symfony\Component\Form\Forms;
4848
use Symfony\Component\HttpFoundation\Request;
4949
use Symfony\Component\Security\Csrf\CsrfTokenManager;
50-
use Twig\Error\Error;
5150
use Twig\Error\SyntaxError;
5251
use Twig\RuntimeLoader\FactoryRuntimeLoader;
5352
use utils;
@@ -481,24 +480,31 @@ public function DisplayPage($aParams = array(), $sTemplateName = null, $sPageTyp
481480
}
482481
$aParams = array_merge($this->GetDefaultParameters(), $aParams);
483482
$this->CreatePage($sPageType);
484-
$sHTMLContent = $this->RenderTemplate($aParams, $sTemplateName, 'html');
483+
$sHTMLContent = $this->RenderTemplate($aParams, $sTemplateName, 'html', $sErrorMsg);
485484
if ($sHTMLContent !== false) {
486485
$this->AddToPage($sHTMLContent);
487486
}
488-
$sJSScript = $this->RenderTemplate($aParams, $sTemplateName, 'js');
487+
$sJSScript = $this->RenderTemplate($aParams, $sTemplateName, 'js', $sErrorMsg);
489488
if ($sJSScript !== false) {
490489
$this->AddScriptToPage($sJSScript);
491490
}
492-
$sReadyScript = $this->RenderTemplate($aParams, $sTemplateName, 'ready.js');
491+
$sReadyScript = $this->RenderTemplate($aParams, $sTemplateName, 'ready.js', $sErrorMsg);
493492
if ($sReadyScript !== false) {
494493
$this->AddReadyScriptToPage($sReadyScript);
495494
}
496-
$sStyle = $this->RenderTemplate($aParams, $sTemplateName, 'css');
495+
$sStyle = $this->RenderTemplate($aParams, $sTemplateName, 'css', $sErrorMsg);
497496
if ($sStyle !== false) {
498497
$this->AddStyleToPage($sStyle);
499498
}
500499
if ($sHTMLContent === false && $sJSScript === false && $sReadyScript === false && $sStyle === false) {
501-
IssueLog::Error("Missing TWIG template for $sTemplateName");
500+
if (utils::IsNullOrEmptyString($sErrorMsg)) {
501+
$sErrorMsg = "Missing TWIG template for $sTemplateName";
502+
}
503+
IssueLog::Error($sErrorMsg);
504+
$this->AddToPage($this->m_oTwig->render('application/forms/itop_error.html.twig', ['sControllerError' => $sErrorMsg]));
505+
}
506+
if (utils::IsDevelopmentEnvironment()) {
507+
$this->AddToPage($this->m_oTwig->render('application/forms/itop_debug.html.twig', ['sDebugData' => $this->GetDebugData($aParams)]));
502508
}
503509
if (!empty($this->m_aAjaxTabs)) {
504510
$this->m_oPage->AddTabContainer('TwigBaseTabContainer');
@@ -764,36 +770,46 @@ public function GetForm(string $type = FormType::class, mixed $data = null, arra
764770
* @return string|false
765771
* @throws \Exception
766772
*/
767-
private function RenderTemplate($aParams, $sName, $sTemplateFileExtension)
773+
private function RenderTemplate(array $aParams, string $sName, string $sTemplateFileExtension, string &$sErrorMsg = null): string|false
768774
{
775+
$sTemplateFile = $sName.'.'.$sTemplateFileExtension.'.twig';
769776
if (empty($this->m_oTwig))
770777
{
771778
throw new Exception('Not initialized. Call Controller::InitFromModule() or Controller::SetViewPath() before any display');
772779
}
773780
try
774781
{
775-
return $this->m_oTwig->render($sName.'.'.$sTemplateFileExtension.'.twig', $aParams);
782+
return $this->m_oTwig->render($sTemplateFile, $aParams);
776783
}
777784
catch (SyntaxError $e) {
778785
IssueLog::Error($e->getMessage().' - file: '.$e->getFile().'('.$e->getLine().')');
779786
return $this->m_oTwig->render('application/forms/itop_error.html.twig', ['sControllerError' => $e->getMessage()]);
780787
}
781-
catch (Error $e) {
782-
if (strpos($e->getMessage(), 'Unable to find template') === false)
788+
catch (Exception $e) {
789+
$sExceptionMessage = $e->getMessage();
790+
if (str_contains($sExceptionMessage, 'at line')) {
791+
IssueLog::Error($sExceptionMessage);
792+
return $this->m_oTwig->render('application/forms/itop_error.html.twig', ['sControllerError' => $sExceptionMessage]);
793+
}
794+
if (!str_contains($sExceptionMessage, 'Unable to find template'))
783795
{
784-
IssueLog::Error($e->getMessage());
796+
IssueLog::Error($sExceptionMessage);
797+
}
798+
if (is_null($sErrorMsg)) {
799+
$sErrorMsg = '';
785800
}
801+
$sErrorMsg .= $sExceptionMessage."\n";
786802
}
787803

788804
return false;
789805
}
790806

791807
/**
792-
* @param $sPageType
808+
* @param string $sPageType
793809
*
794810
* @throws \Exception
795811
*/
796-
private function CreatePage($sPageType)
812+
private function CreatePage(string $sPageType): void
797813
{
798814
switch ($sPageType)
799815
{
@@ -910,4 +926,25 @@ private function OutputPage()
910926
{
911927
$this->m_oPage->output();
912928
}
929+
930+
/**
931+
* @param array $aParams
932+
*
933+
* @return string
934+
*/
935+
public function GetDebugData(array $aParams): string
936+
{
937+
$sDebugData = '';
938+
foreach ($aParams as $sKey => $mValue) {
939+
if (is_object($mValue)) {
940+
$sDebugData .= "$sKey: Object...\n";
941+
} else {
942+
$sDebugData .= "$sKey: ".var_export($mValue, true)."\n";
943+
}
944+
}
945+
$sStackTrace = (new Exception(''))->getTraceAsString();
946+
$sDebugData .= "Stack trace:\n$sStackTrace";
947+
948+
return $sDebugData;
949+
}
913950
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{# @copyright Copyright (C) 2010-2025 Combodo SARL #}
2+
{# @license http://opensource.org/licenses/AGPL-3.0 #}
3+
4+
{% if sDebugData %}
5+
{% UICollapsibleSection Standard { sTitle:'Debug context', OpenedByDefault:false } %}
6+
{% UIContentBlock ForCode { sCode:sDebugData } %}{% EndUIContentBlock %}
7+
{% EndUICollapsibleSection %}
8+
{% endif %}
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
{# @copyright Copyright (C) 2010-2025 Combodo SARL #}
22
{# @license http://opensource.org/licenses/AGPL-3.0 #}
33

4-
{% if debug %}
5-
{% if sControllerError %}
6-
{% UIContentBlock ForCode { sCode:'' } %}
7-
{{ dump(sControllerError) }}
8-
{% EndUIContentBlock %}
9-
{% endif %}
10-
{% UIContentBlock ForCode { sCode:'' } %}
11-
{{ dump(_context) }}
12-
{% EndUIContentBlock %}
13-
{% else %}
14-
{% if sControllerError and not debug %}
15-
{% UIAlert ForDanger { sTitle:'UI:Error:FormController'|dict_s, sContent:sControllerError } %}{% EndUIAlert %}
16-
{% endif %}
4+
{% if sControllerError %}
5+
{% UIAlert ForDanger { sTitle:'UI:Error:FormController'|dict_s, sContent:sControllerError } %}{% EndUIAlert %}
176
{% endif %}

0 commit comments

Comments
 (0)