Skip to content

Commit 4b64b03

Browse files
committed
TASK: Catch error because of invalid template option
1 parent 7cb8ff4 commit 4b64b03

File tree

7 files changed

+43
-17
lines changed

7 files changed

+43
-17
lines changed

Classes/Domain/Template/RootTemplate.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace Flowpack\NodeTemplates\Domain\Template;
55

6-
use Flowpack\NodeTemplates\Domain\Template\Template;
7-
use Flowpack\NodeTemplates\Domain\Template\Templates;
86
use Neos\Flow\Annotations as Flow;
97

108
/**
@@ -31,6 +29,11 @@ public function __construct(array $properties, Templates $childNodes)
3129
$this->childNodes = $childNodes;
3230
}
3331

32+
public static function empty(): self
33+
{
34+
return new RootTemplate([], Templates::empty());
35+
}
36+
3437
/**
3538
* @return array<string, string>
3639
*/

Classes/Domain/Template/Template.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
namespace Flowpack\NodeTemplates\Domain\Template;
55

6-
use Flowpack\NodeTemplates\Domain\Template\Templates;
7-
use Neos\ContentRepository\Domain\NodeType\NodeTypeName;
86
use Neos\ContentRepository\Domain\NodeAggregate\NodeName;
7+
use Neos\ContentRepository\Domain\NodeType\NodeTypeName;
98
use Neos\Flow\Annotations as Flow;
109

1110
/** @Flow\Proxy(false) */

Classes/Domain/Template/Templates.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Flowpack\NodeTemplates\Domain\Template;
44

5-
use Flowpack\NodeTemplates\Domain\Template\RootTemplate;
6-
use Flowpack\NodeTemplates\Domain\Template\Template;
75
use Neos\Flow\Annotations as Flow;
86

97
/** @Flow\Proxy(false) */
@@ -52,7 +50,7 @@ public function toRootTemplate(): RootTemplate
5250
$first->getChildNodes()
5351
);
5452
}
55-
return new RootTemplate( [], Templates::empty());
53+
return RootTemplate::empty();
5654
}
5755

5856
public function jsonSerialize()

Classes/Domain/TemplateConfiguration/TemplateConfigurationProcessor.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Flowpack\NodeTemplates\Domain\Template\RootTemplate;
88
use Flowpack\NodeTemplates\Domain\Template\Template;
99
use Flowpack\NodeTemplates\Domain\Template\Templates;
10-
use Flowpack\NodeTemplates\Domain\TemplateConfiguration\EelEvaluationService;
1110
use Neos\ContentRepository\Domain\NodeAggregate\NodeName;
1211
use Neos\ContentRepository\Domain\NodeType\NodeTypeName;
1312
use Neos\Flow\Annotations as Flow;
@@ -31,12 +30,16 @@ class TemplateConfigurationProcessor
3130
*/
3231
public function processTemplateConfiguration(array $configuration, array $evaluationContext, CaughtExceptions $caughtEvaluationExceptions): RootTemplate
3332
{
34-
$templatePart = TemplatePart::createRoot(
35-
$configuration,
36-
$evaluationContext,
37-
fn ($value, $evaluationContext) => $this->preprocessConfigurationValue($value, $evaluationContext),
38-
$caughtEvaluationExceptions
39-
);
33+
try {
34+
$templatePart = TemplatePart::createRoot(
35+
$configuration,
36+
$evaluationContext,
37+
fn ($value, $evaluationContext) => $this->preprocessConfigurationValue($value, $evaluationContext),
38+
$caughtEvaluationExceptions
39+
);
40+
} catch (StopBuildingTemplatePartException $e) {
41+
return RootTemplate::empty();
42+
}
4043
return $this->createTemplatesFromTemplatePart($templatePart)->toRootTemplate();
4144
}
4245

@@ -105,7 +108,11 @@ private function createTemplateFromTemplatePart(TemplatePart $templatePart): Tem
105108
// process the childNodes
106109
$childNodeTemplates = Templates::empty();
107110
foreach ($templatePart->getRawConfiguration('childNodes') ?? [] as $childNodeConfigurationPath => $_) {
108-
$childNodeTemplatePart = $templatePart->withConfigurationByConfigurationPath(['childNodes', $childNodeConfigurationPath]);
111+
try {
112+
$childNodeTemplatePart = $templatePart->withConfigurationByConfigurationPath(['childNodes', $childNodeConfigurationPath]);
113+
} catch (StopBuildingTemplatePartException $e) {
114+
continue;
115+
}
109116
$childNodeTemplates = $childNodeTemplates->merge($this->createTemplatesFromTemplatePart($childNodeTemplatePart));
110117
}
111118

Classes/Domain/TemplateConfiguration/TemplatePart.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TemplatePart
4343
* @psalm-param array<string, mixed> $configuration
4444
* @psalm-param array<string, mixed> $evaluationContext
4545
* @psalm-param \Closure(mixed $value, array<string, mixed> $evaluationContext): mixed $configurationValueProcessor
46+
* @throws StopBuildingTemplatePartException
4647
*/
4748
private function __construct(
4849
array $configuration,
@@ -63,6 +64,7 @@ private function __construct(
6364
* @psalm-param array<string, mixed> $configuration
6465
* @psalm-param array<string, mixed> $evaluationContext
6566
* @psalm-param \Closure(mixed $value, array<string, mixed> $evaluationContext): mixed $configurationValueProcessor
67+
* @throws StopBuildingTemplatePartException
6668
*/
6769
public static function createRoot(
6870
array $configuration,
@@ -91,6 +93,7 @@ public function getFullPathToConfiguration(): array
9193

9294
/**
9395
* @psalm-param string|list<string> $configurationPath
96+
* @throws StopBuildingTemplatePartException
9497
*/
9598
public function withConfigurationByConfigurationPath($configurationPath): self
9699
{
@@ -188,16 +191,25 @@ public function hasConfiguration($configurationPath): bool
188191
return true;
189192
}
190193

194+
/**
195+
* @throws StopBuildingTemplatePartException
196+
*/
191197
private function validateTemplateConfigurationKeys(): void
192198
{
193199
$isRootTemplate = $this->fullPathToConfiguration === [];
194200
foreach (array_keys($this->configuration) as $key) {
195201
if (!in_array($key, ['type', 'name', 'properties', 'childNodes', 'when', 'withItems', 'withContext'], true)) {
196-
throw new \InvalidArgumentException(sprintf('Template configuration has illegal key "%s"', $key));
202+
$this->caughtExceptions->add(
203+
CaughtException::fromException(new \InvalidArgumentException(sprintf('Template configuration has illegal key "%s"', $key), 1686150349274))
204+
);
205+
throw new StopBuildingTemplatePartException();
197206
}
198207
if ($isRootTemplate) {
199208
if (!in_array($key, ['properties', 'childNodes', 'when', 'withContext'], true)) {
200-
throw new \InvalidArgumentException(sprintf('Root template configuration doesnt allow option "%s', $key));
209+
$this->caughtExceptions->add(
210+
CaughtException::fromException(new \InvalidArgumentException(sprintf('Root template configuration doesnt allow option "%s', $key), 1686150340657))
211+
);
212+
throw new StopBuildingTemplatePartException();
201213
}
202214
}
203215
}

Configuration/Testing/NodeTypes.Malformed.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
working: "working"
4444
nonDeclaredProperty: "hi"
4545
bar: "${'left open"
46+
# only simple scalar types
4647
nonEelArrayNotAllowed:
4748
not: allowed
4849
childNodes:
@@ -90,3 +91,5 @@
9091
typeCantMutate:
9192
name: "type-cant-mutate"
9293
type: "Flowpack.NodeTemplates:Content.WithEvaluationExceptions"
94+
invalidOption:
95+
crazy: me

Tests/Functional/Fixtures/WithEvaluationExceptions.messages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
"message": "Configuration \"null\" in \"childNodes.withItemsAbortBecauseNotIterable.withItems\" | RuntimeException(Type NULL is not iterable., 1685802354186)",
4848
"severity": "ERROR"
4949
},
50+
{
51+
"message": "InvalidArgumentException(Template configuration has illegal key \"crazy\", 1686150349274)",
52+
"severity": "ERROR"
53+
},
5054
{
5155
"message": "Property \"_hidden\" in NodeType \"Flowpack.NodeTemplates:Content.WithEvaluationExceptions\" | PropertyIgnoredException(Because internal legacy property \"_hidden\" not implement., 1686149513158)",
5256
"severity": "ERROR"

0 commit comments

Comments
 (0)