|
47 | 47 | use Symfony\Component\Form\Forms; |
48 | 48 | use Symfony\Component\HttpFoundation\Request; |
49 | 49 | use Symfony\Component\Security\Csrf\CsrfTokenManager; |
50 | | -use Twig\Error\Error; |
51 | 50 | use Twig\Error\SyntaxError; |
52 | 51 | use Twig\RuntimeLoader\FactoryRuntimeLoader; |
53 | 52 | use utils; |
@@ -481,24 +480,31 @@ public function DisplayPage($aParams = array(), $sTemplateName = null, $sPageTyp |
481 | 480 | } |
482 | 481 | $aParams = array_merge($this->GetDefaultParameters(), $aParams); |
483 | 482 | $this->CreatePage($sPageType); |
484 | | - $sHTMLContent = $this->RenderTemplate($aParams, $sTemplateName, 'html'); |
| 483 | + $sHTMLContent = $this->RenderTemplate($aParams, $sTemplateName, 'html', $sErrorMsg); |
485 | 484 | if ($sHTMLContent !== false) { |
486 | 485 | $this->AddToPage($sHTMLContent); |
487 | 486 | } |
488 | | - $sJSScript = $this->RenderTemplate($aParams, $sTemplateName, 'js'); |
| 487 | + $sJSScript = $this->RenderTemplate($aParams, $sTemplateName, 'js', $sErrorMsg); |
489 | 488 | if ($sJSScript !== false) { |
490 | 489 | $this->AddScriptToPage($sJSScript); |
491 | 490 | } |
492 | | - $sReadyScript = $this->RenderTemplate($aParams, $sTemplateName, 'ready.js'); |
| 491 | + $sReadyScript = $this->RenderTemplate($aParams, $sTemplateName, 'ready.js', $sErrorMsg); |
493 | 492 | if ($sReadyScript !== false) { |
494 | 493 | $this->AddReadyScriptToPage($sReadyScript); |
495 | 494 | } |
496 | | - $sStyle = $this->RenderTemplate($aParams, $sTemplateName, 'css'); |
| 495 | + $sStyle = $this->RenderTemplate($aParams, $sTemplateName, 'css', $sErrorMsg); |
497 | 496 | if ($sStyle !== false) { |
498 | 497 | $this->AddStyleToPage($sStyle); |
499 | 498 | } |
500 | 499 | 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)])); |
502 | 508 | } |
503 | 509 | if (!empty($this->m_aAjaxTabs)) { |
504 | 510 | $this->m_oPage->AddTabContainer('TwigBaseTabContainer'); |
@@ -764,36 +770,46 @@ public function GetForm(string $type = FormType::class, mixed $data = null, arra |
764 | 770 | * @return string|false |
765 | 771 | * @throws \Exception |
766 | 772 | */ |
767 | | - private function RenderTemplate($aParams, $sName, $sTemplateFileExtension) |
| 773 | + private function RenderTemplate(array $aParams, string $sName, string $sTemplateFileExtension, string &$sErrorMsg = null): string|false |
768 | 774 | { |
| 775 | + $sTemplateFile = $sName.'.'.$sTemplateFileExtension.'.twig'; |
769 | 776 | if (empty($this->m_oTwig)) |
770 | 777 | { |
771 | 778 | throw new Exception('Not initialized. Call Controller::InitFromModule() or Controller::SetViewPath() before any display'); |
772 | 779 | } |
773 | 780 | try |
774 | 781 | { |
775 | | - return $this->m_oTwig->render($sName.'.'.$sTemplateFileExtension.'.twig', $aParams); |
| 782 | + return $this->m_oTwig->render($sTemplateFile, $aParams); |
776 | 783 | } |
777 | 784 | catch (SyntaxError $e) { |
778 | 785 | IssueLog::Error($e->getMessage().' - file: '.$e->getFile().'('.$e->getLine().')'); |
779 | 786 | return $this->m_oTwig->render('application/forms/itop_error.html.twig', ['sControllerError' => $e->getMessage()]); |
780 | 787 | } |
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')) |
783 | 795 | { |
784 | | - IssueLog::Error($e->getMessage()); |
| 796 | + IssueLog::Error($sExceptionMessage); |
| 797 | + } |
| 798 | + if (is_null($sErrorMsg)) { |
| 799 | + $sErrorMsg = ''; |
785 | 800 | } |
| 801 | + $sErrorMsg .= $sExceptionMessage."\n"; |
786 | 802 | } |
787 | 803 |
|
788 | 804 | return false; |
789 | 805 | } |
790 | 806 |
|
791 | 807 | /** |
792 | | - * @param $sPageType |
| 808 | + * @param string $sPageType |
793 | 809 | * |
794 | 810 | * @throws \Exception |
795 | 811 | */ |
796 | | - private function CreatePage($sPageType) |
| 812 | + private function CreatePage(string $sPageType): void |
797 | 813 | { |
798 | 814 | switch ($sPageType) |
799 | 815 | { |
@@ -910,4 +926,25 @@ private function OutputPage() |
910 | 926 | { |
911 | 927 | $this->m_oPage->output(); |
912 | 928 | } |
| 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 | + } |
913 | 950 | } |
0 commit comments