N°3522 Display error alert for twig rendering errors

This commit is contained in:
Pierre Goiffon
2021-03-08 16:08:06 +01:00
parent 1a3e8c59c7
commit c5021721c6
3 changed files with 30 additions and 4 deletions

View File

@@ -460,6 +460,7 @@ Dict::Add('EN US', 'English', 'English', array(
'UI:Error:MaintenanceMode' => 'Application is currently in maintenance',
'UI:Error:MaintenanceTitle' => 'Maintenance',
'UI:Error:InvalidToken' => 'Error: the requested operation has already been performed (CSRF token not found)',
'UI:Error:TemplateRendering' => 'Cannot render template',
'UI:GroupBy:Count' => 'Count',
'UI:GroupBy:Count+' => 'Number of elements',

View File

@@ -456,6 +456,7 @@ Dict::Add('FR FR', 'French', 'Français', array(
'UI:Error:MaintenanceMode' => 'L\'application est en maintenance',
'UI:Error:MaintenanceTitle' => 'Maintenance',
'UI:Error:InvalidToken' => 'Erreur: l\'opération a déjà été effectuée (CSRF token not found)',
'UI:Error:TemplateRendering' => 'Impossible de rendre le template',
'UI:GroupBy:Count' => 'Nombre',
'UI:GroupBy:Count+' => 'Nombre d\'éléments',

View File

@@ -7,7 +7,9 @@
namespace Combodo\iTop\Application\TwigBase\Twig;
use Combodo\iTop\Application\TwigBase\UI\UIBlockExtension;
use Exception;
use Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory;
use Combodo\iTop\Renderer\BlockRenderer;
use Dict;
use IssueLog;
use Twig\Environment;
use Twig\Error\Error;
@@ -128,15 +130,37 @@ class TwigHelper
$sPath = utils::LocalPath($e->getSourceContext()->getPath()).' ('.$e->getLine().') - ';
}
$sMessage = $sPath.$e->getMessage();
if (strpos($e->getMessage(), 'Unable to find template') === false) {
IssueLog::Error($sMessage);
// Todo 3.0 Less violent message
throw new Exception($sMessage);
} elseif ($bLogMissingFile) {
return static::GenerateEndUserError(Dict::S('UI:Error:TemplateRendering'), $sMessage);
}
if ($bLogMissingFile) {
IssueLog::Debug($sMessage);
}
}
return '';
}
/**
* @param string $sTitle
* @param string $sMessage
*
* @return string error panel markup
* @throws \ReflectionException
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
protected static function GenerateEndUserError(string $sTitle, string $sMessage): string
{
$oAlert = AlertUIBlockFactory::MakeForFailure($sTitle, $sMessage)
->SetIsClosable(false)
->SetIsCollapsible(false); // not rendering JS so...
return BlockRenderer::RenderBlockTemplates($oAlert);
}
}