Skip to content

Commit

Permalink
TASK: Improve JSON error handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bwaidelich committed Aug 7, 2024
1 parent b2d5f28 commit bab49a9
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,17 @@ public function jsonSerialize(): array

/**
* @return array<string, string>
* @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,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -244,7 +249,6 @@ private function processNodeData(array $nodeDataRow): void
* @param NodeAggregateId $nodeAggregateId
* @param array<string, mixed> $nodeDataRow
* @return NodeName[]|void
* @throws \JsonException
*/
public function processNodeDataWithoutFallbackToEmptyDimension(NodeAggregateId $nodeAggregateId, OriginDimensionSpacePoint $originDimensionSpacePoint, array $nodeDataRow)
{
Expand Down
2 changes: 0 additions & 2 deletions Neos.Workspace.Ui/Classes/Controller/WorkspaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,int>
* @throws \JsonException
*/
protected function computeChangesCount(Workspace $selectedWorkspace, ContentRepository $contentRepository): array
{
Expand All @@ -751,7 +750,6 @@ protected function computeChangesCount(Workspace $selectedWorkspace, ContentRepo
/**
* Builds an array of changes for sites in the given workspace
* @return array<string,mixed>
* @throws \JsonException
*/
protected function computeSiteChanges(Workspace $selectedWorkspace, ContentRepository $contentRepository): array
{
Expand Down

0 comments on commit bab49a9

Please sign in to comment.