Skip to content

Commit eb75f72

Browse files
committed
TASK: Known exceptions are logged to the Neos.Ui and caught
1 parent 0afac83 commit eb75f72

3 files changed

Lines changed: 84 additions & 7 deletions

File tree

Classes/NodeCreationHandler/TemplateNodeCreationHandler.php

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace Flowpack\NodeTemplates\NodeCreationHandler;
44

5-
use Neos\Flow\Annotations as Flow;
5+
use Flowpack\NodeTemplates\Service\EelException;
66
use Flowpack\NodeTemplates\Template;
77
use Neos\ContentRepository\Domain\Model\NodeInterface;
8+
use Neos\ContentRepository\Exception\NodeConstraintException;
9+
use Neos\Flow\Annotations as Flow;
810
use Neos\Flow\Property\PropertyMapper;
11+
use Neos\Neos\Ui\Domain\Model\Feedback\Messages\Error;
12+
use Neos\Neos\Ui\Domain\Model\FeedbackCollection;
913
use Neos\Neos\Ui\NodeCreationHandler\NodeCreationHandlerInterface;
1014

11-
1215
class TemplateNodeCreationHandler implements NodeCreationHandlerInterface
1316
{
1417
/**
@@ -23,6 +26,12 @@ class TemplateNodeCreationHandler implements NodeCreationHandlerInterface
2326
*/
2427
protected $nodeCreationDepth;
2528

29+
/**
30+
* @var FeedbackCollection
31+
* @Flow\Inject
32+
*/
33+
protected $feedbackCollection;
34+
2635
/**
2736
* Create child nodes and change properties upon node creation
2837
*
@@ -47,13 +56,74 @@ public function handle(NodeInterface $node, array $data): void
4756
}
4857

4958
/** @var Template $template */
50-
$template = $this->propertyMapper->convert($templateConfiguration, Template::class,
51-
$propertyMappingConfiguration);
59+
$template = $this->propertyMapper->convert(
60+
$templateConfiguration,
61+
Template::class,
62+
$propertyMappingConfiguration
63+
);
5264

5365
$context = [
5466
'data' => $data,
5567
'triggeringNode' => $node,
5668
];
57-
$template->apply($node, $context);
69+
70+
try {
71+
$template->apply($node, $context);
72+
} catch (\Exception $exception) {
73+
$this->handleExceptions($node, $exception);
74+
}
75+
}
76+
77+
/**
78+
* Known exceptions are logged to the Neos.Ui and caught
79+
*
80+
* @param NodeInterface $node the newly created node
81+
* @param \Exception $exception the exception to handle
82+
* @throws \Exception in case the exception is unknown and cant be handled
83+
*/
84+
private function handleExceptions(NodeInterface $node, \Exception $exception): void
85+
{
86+
$nodeTemplateError = new Error();
87+
$nodeTemplateError->setMessage(sprintf('Template for "%s" only partially applied. Please check the newly created nodes.', $node->getNodeType()->getLabel()));
88+
89+
if ($exception instanceof NodeConstraintException) {
90+
$this->feedbackCollection->add(
91+
$nodeTemplateError
92+
);
93+
94+
$error = new Error();
95+
$error->setMessage($exception->getMessage());
96+
$this->feedbackCollection->add(
97+
$error
98+
);
99+
return;
100+
}
101+
if ($exception instanceof EelException) {
102+
$this->feedbackCollection->add(
103+
$nodeTemplateError
104+
);
105+
106+
$error = new Error();
107+
$error->setMessage(
108+
$exception->getMessage()
109+
);
110+
$this->feedbackCollection->add(
111+
$error
112+
);
113+
114+
$level = 0;
115+
while (($exception = $exception->getPrevious()) && $level <= 8) {
116+
$level++;
117+
$error = new Error();
118+
$error->setMessage(
119+
sprintf('%s [%s(%s)]', $exception->getMessage(), get_class($exception), $exception->getCode())
120+
);
121+
$this->feedbackCollection->add(
122+
$error
123+
);
124+
}
125+
return;
126+
}
127+
throw $exception;
58128
}
59129
}

Classes/Service/EelEvaluationService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function evaluateEelExpression(string $expression, array $contextVariable
4444
try {
4545
return EelUtility::evaluateEelExpression($expression, $this->eelEvaluator, $contextVariables);
4646
} catch (ParserException $parserException) {
47-
throw new \RuntimeException('EEL Expression in NodeType template could not be parsed.', 1684788574212, $parserException);
47+
throw new EelException('EEL Expression in NodeType template could not be parsed.', 1684788574212, $parserException);
4848
} catch (\Exception $exception) {
49-
throw new \RuntimeException(sprintf('EEL Expression "%s" in NodeType template caused an error.', $expression), 1684761760723, $exception);
49+
throw new EelException(sprintf('EEL Expression "%s" in NodeType template caused an error.', $expression), 1684761760723, $exception);
5050
}
5151
}
5252
}

Classes/Service/EelException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Flowpack\NodeTemplates\Service;
4+
5+
class EelException extends \Exception
6+
{
7+
}

0 commit comments

Comments
 (0)