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