From bab49a941226a67e1d45d3c24cd6204891be54ab Mon Sep 17 00:00:00 2001 From: Bastian Waidelich Date: Wed, 7 Aug 2024 14:16:52 +0200 Subject: [PATCH] TASK: Improve JSON error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces all `@throws JsonException` annotations with a try/catch block. Background: Adding the `@throws` tag means that this exception has to be handled in the calling side. But in the affected cases the JSON that is to be en/decoded does not come from a foreign source. So an exception in those cases is very unlikely – i.e. exceptional. Besides, throwing an expception with more context, will make ease debugging of those cases --- .../DimensionSpace/AbstractDimensionSpacePoint.php | 9 +++++---- .../DimensionSpace/ContentSubgraphVariationWeight.php | 9 +++++---- .../Classes/DimensionSpace/DimensionSpacePointSet.php | 9 +++++---- .../NodeCreation/Dto/NodeAggregateIdsByNodePaths.php | 9 +++++---- .../NodeModification/Dto/PropertyValuesToWrite.php | 9 +++++---- .../NodeModification/Dto/SerializedPropertyValue.php | 8 ++++++-- .../NodeReferencing/Dto/NodeReferencesToWrite.php | 9 +++++---- .../Classes/NodeDataToEventsProcessor.php | 8 ++++++-- .../Classes/Controller/WorkspaceController.php | 2 -- 9 files changed, 42 insertions(+), 30 deletions(-) diff --git a/Neos.ContentRepository.Core/Classes/DimensionSpace/AbstractDimensionSpacePoint.php b/Neos.ContentRepository.Core/Classes/DimensionSpace/AbstractDimensionSpacePoint.php index 9f457b03576..60d97bb9b9d 100644 --- a/Neos.ContentRepository.Core/Classes/DimensionSpace/AbstractDimensionSpacePoint.php +++ b/Neos.ContentRepository.Core/Classes/DimensionSpace/AbstractDimensionSpacePoint.php @@ -145,11 +145,12 @@ final public function jsonSerialize(): array return $this->coordinates; } - /** - * @throws \JsonException - */ final public function toJson(): string { - return json_encode($this, JSON_THROW_ON_ERROR); + try { + return json_encode($this, JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + throw new \RuntimeException(sprintf('Failed to JSON-encode %s: %s', static::class, $e->getMessage()), 1723031263, $e); + } } } diff --git a/Neos.ContentRepository.Core/Classes/DimensionSpace/ContentSubgraphVariationWeight.php b/Neos.ContentRepository.Core/Classes/DimensionSpace/ContentSubgraphVariationWeight.php index 29c34cfe7f4..c05c604acc4 100644 --- a/Neos.ContentRepository.Core/Classes/DimensionSpace/ContentSubgraphVariationWeight.php +++ b/Neos.ContentRepository.Core/Classes/DimensionSpace/ContentSubgraphVariationWeight.php @@ -105,11 +105,12 @@ public function jsonSerialize(): array return $this->value; } - /** - * @throws \JsonException - */ public function toJson(): string { - return json_encode($this, JSON_THROW_ON_ERROR); + try { + return json_encode($this, JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + throw new \RuntimeException(sprintf('Failed to JSON-encode %s: %s', self::class, $e->getMessage()), 1723031892, $e); + } } } diff --git a/Neos.ContentRepository.Core/Classes/DimensionSpace/DimensionSpacePointSet.php b/Neos.ContentRepository.Core/Classes/DimensionSpace/DimensionSpacePointSet.php index 56179626e7d..b7dbf4de6e5 100644 --- a/Neos.ContentRepository.Core/Classes/DimensionSpace/DimensionSpacePointSet.php +++ b/Neos.ContentRepository.Core/Classes/DimensionSpace/DimensionSpacePointSet.php @@ -156,11 +156,12 @@ public function getIterator(): \Traversable yield from $this->points; } - /** - * @throws \JsonException - */ public function toJson(): string { - return json_encode($this, JSON_THROW_ON_ERROR); + try { + return json_encode($this, JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + throw new \RuntimeException(sprintf('Failed to JSON-encode %s: %s', self::class, $e->getMessage()), 1723031979, $e); + } } } diff --git a/Neos.ContentRepository.Core/Classes/Feature/NodeCreation/Dto/NodeAggregateIdsByNodePaths.php b/Neos.ContentRepository.Core/Classes/Feature/NodeCreation/Dto/NodeAggregateIdsByNodePaths.php index 083a4cfb607..f4cd272b9ee 100644 --- a/Neos.ContentRepository.Core/Classes/Feature/NodeCreation/Dto/NodeAggregateIdsByNodePaths.php +++ b/Neos.ContentRepository.Core/Classes/Feature/NodeCreation/Dto/NodeAggregateIdsByNodePaths.php @@ -101,12 +101,13 @@ public static function fromArray(array $array): self return new self($nodeAggregateIds); } - /** - * @throws \JsonException - */ public static function fromJsonString(string $jsonString): self { - return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR)); + try { + return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR)); + } catch (\JsonException $e) { + throw new \RuntimeException(sprintf('Failed to JSON-decode "%s": %s', $jsonString, $e->getMessage()), 1723032037, $e); + } } public function merge(self $other): self diff --git a/Neos.ContentRepository.Core/Classes/Feature/NodeModification/Dto/PropertyValuesToWrite.php b/Neos.ContentRepository.Core/Classes/Feature/NodeModification/Dto/PropertyValuesToWrite.php index 8ab899ebdd7..dcc0f414f3c 100644 --- a/Neos.ContentRepository.Core/Classes/Feature/NodeModification/Dto/PropertyValuesToWrite.php +++ b/Neos.ContentRepository.Core/Classes/Feature/NodeModification/Dto/PropertyValuesToWrite.php @@ -51,12 +51,13 @@ public static function fromArray(array $values): self return new self($values); } - /** - * @throws \JsonException - */ public static function fromJsonString(string $jsonString): self { - return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR)); + try { + return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR)); + } catch (\JsonException $e) { + throw new \RuntimeException(sprintf('Failed to JSON-decode "%s": %s', $jsonString, $e->getMessage()), 1723032130, $e); + } } /** diff --git a/Neos.ContentRepository.Core/Classes/Feature/NodeModification/Dto/SerializedPropertyValue.php b/Neos.ContentRepository.Core/Classes/Feature/NodeModification/Dto/SerializedPropertyValue.php index 650652adb12..21d9424bfbf 100644 --- a/Neos.ContentRepository.Core/Classes/Feature/NodeModification/Dto/SerializedPropertyValue.php +++ b/Neos.ContentRepository.Core/Classes/Feature/NodeModification/Dto/SerializedPropertyValue.php @@ -77,13 +77,17 @@ public function jsonSerialize(): array /** * @return array - * @throws \JsonException */ public function __debugInfo(): array { + try { + $valueAsJson = json_encode($this->value, JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + throw new \RuntimeException(sprintf('Failed to JSON-encode %s: %s', self::class, $e->getMessage()), 1723032361, $e); + } return [ 'type' => $this->type, - 'value' => json_encode($this->value, JSON_THROW_ON_ERROR) + 'value' => $valueAsJson, ]; } } diff --git a/Neos.ContentRepository.Core/Classes/Feature/NodeReferencing/Dto/NodeReferencesToWrite.php b/Neos.ContentRepository.Core/Classes/Feature/NodeReferencing/Dto/NodeReferencesToWrite.php index 3fefefbde63..02bea2b6115 100644 --- a/Neos.ContentRepository.Core/Classes/Feature/NodeReferencing/Dto/NodeReferencesToWrite.php +++ b/Neos.ContentRepository.Core/Classes/Feature/NodeReferencing/Dto/NodeReferencesToWrite.php @@ -75,12 +75,13 @@ public static function fromNodeAggregateIds(NodeAggregateIds $nodeAggregateIds): )); } - /** - * @throws \JsonException - */ public static function fromJsonString(string $jsonString): self { - return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR)); + try { + return self::fromArray(\json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR)); + } catch (\JsonException $e) { + throw new \RuntimeException(sprintf('Failed to JSON-decode "%s": %s', $jsonString, $e->getMessage()), 1723032146, $e); + } } /** diff --git a/Neos.ContentRepository.LegacyNodeMigration/Classes/NodeDataToEventsProcessor.php b/Neos.ContentRepository.LegacyNodeMigration/Classes/NodeDataToEventsProcessor.php index 56aaa7fbad7..9379206d5ee 100644 --- a/Neos.ContentRepository.LegacyNodeMigration/Classes/NodeDataToEventsProcessor.php +++ b/Neos.ContentRepository.LegacyNodeMigration/Classes/NodeDataToEventsProcessor.php @@ -169,10 +169,15 @@ private function resetRuntimeState(): void private function exportEvent(EventInterface $event): void { $normalizedEvent = $this->eventNormalizer->normalize($event); + try { + $exportedEventPayload = json_decode($normalizedEvent->data->value, true, 512, JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + throw new \RuntimeException(sprintf('Failed to JSON-decode "%s": %s', $normalizedEvent->data->value, $e->getMessage()), 1723032243, $e); + } $exportedEvent = new ExportedEvent( $normalizedEvent->id->value, $normalizedEvent->type->value, - json_decode($normalizedEvent->data->value, true), + $exportedEventPayload, [], ); assert($this->eventFileResource !== null); @@ -244,7 +249,6 @@ private function processNodeData(array $nodeDataRow): void * @param NodeAggregateId $nodeAggregateId * @param array $nodeDataRow * @return NodeName[]|void - * @throws \JsonException */ public function processNodeDataWithoutFallbackToEmptyDimension(NodeAggregateId $nodeAggregateId, OriginDimensionSpacePoint $originDimensionSpacePoint, array $nodeDataRow) { diff --git a/Neos.Workspace.Ui/Classes/Controller/WorkspaceController.php b/Neos.Workspace.Ui/Classes/Controller/WorkspaceController.php index 3c2a6d93d14..c2f64c39e54 100644 --- a/Neos.Workspace.Ui/Classes/Controller/WorkspaceController.php +++ b/Neos.Workspace.Ui/Classes/Controller/WorkspaceController.php @@ -725,7 +725,6 @@ public function discardWorkspaceAction(WorkspaceName $workspace): void * Computes the number of added, changed and removed nodes for the given workspace * * @return array - * @throws \JsonException */ protected function computeChangesCount(Workspace $selectedWorkspace, ContentRepository $contentRepository): array { @@ -751,7 +750,6 @@ protected function computeChangesCount(Workspace $selectedWorkspace, ContentRepo /** * Builds an array of changes for sites in the given workspace * @return array - * @throws \JsonException */ protected function computeSiteChanges(Workspace $selectedWorkspace, ContentRepository $contentRepository): array {