Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Improve JSON error handling #5196

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading