-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathExceptionHandler.php
107 lines (87 loc) · 3.35 KB
/
ExceptionHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace Flowpack\NodeTemplates\Domain\ExceptionHandling;
use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Neos\Ui\Domain\Model\Feedback\Messages\Error;
use Neos\Neos\Ui\Domain\Model\FeedbackCollection;
use Psr\Log\LoggerInterface;
class ExceptionHandler
{
/**
* @var FeedbackCollection
* @Flow\Inject(lazy=false)
*/
protected $feedbackCollection;
/**
* @var LoggerInterface
* @Flow\Inject
*/
protected $logger;
/**
* @var ThrowableStorageInterface
* @Flow\Inject
*/
protected $throwableStorage;
/**
* @var ExceptionHandlingConfiguration
* @Flow\Inject
*/
protected $configuration;
public function handleAfterTemplateConfigurationProcessing(CaughtExceptions $caughtExceptions, NodeInterface $node): void
{
if (!$caughtExceptions->hasExceptions()) {
return;
}
if (!$this->configuration->shouldStopOnExceptionAfterTemplateConfigurationProcessing()) {
return;
}
$templateNotCreatedException = new TemplateNotCreatedException(
sprintf('Template for "%s" was not applied. Only %s was created.', $node->getNodeType()->getLabel(), (string)$node),
1686135532992,
$caughtExceptions->first()->getException(),
);
$this->logCaughtExceptions($caughtExceptions, $templateNotCreatedException);
throw $templateNotCreatedException;
}
public function handleAfterNodeCreation(CaughtExceptions $caughtExceptions, NodeInterface $node): void
{
if (!$caughtExceptions->hasExceptions()) {
return;
}
$templatePartiallyCreatedException = new TemplatePartiallyCreatedException(
sprintf('Template for "%s" only partially applied. Please check the newly created nodes beneath %s.', $node->getNodeType()->getLabel(), (string)$node),
1686135564160,
$caughtExceptions->first()->getException(),
);
$this->logCaughtExceptions($caughtExceptions, $templatePartiallyCreatedException);
throw $templatePartiallyCreatedException;
}
/**
* @param TemplateNotCreatedException|TemplatePartiallyCreatedException $templateCreationException
*/
private function logCaughtExceptions(CaughtExceptions $caughtExceptions, \DomainException $templateCreationException): void
{
$messages = [];
foreach ($caughtExceptions as $index => $caughtException) {
$messages[sprintf('CaughtException (%s)', $index)] = $caughtException->toMessage();
}
// log exception
$messageWithReference = $this->throwableStorage->logThrowable($templateCreationException, $messages);
$this->logger->warning($messageWithReference, LogEnvironment::fromMethodName(__METHOD__));
// neos ui logging
$nodeTemplateError = new Error();
$nodeTemplateError->setMessage($templateCreationException->getMessage());
$this->feedbackCollection->add(
$nodeTemplateError
);
foreach ($messages as $message) {
$error = new Error();
$error->setMessage($message);
$this->feedbackCollection->add(
$error
);
}
}
}