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

Handle JSON errors when decoding #68

Merged
merged 3 commits into from
Mar 31, 2020
Merged
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion src/DeferredImageStorageFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ private function getConfigPath(string $path): string
*/
private function decode(string $contents): array
{
return json_decode($contents, true);
$content = json_decode($contents, true);

if (!\is_array($content)) {
throw new \RuntimeException('Invalid JSON: '.json_last_error_msg());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!\is_array($content)) {
throw new \RuntimeException('Invalid JSON: '.json_last_error_msg());
}
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \JsonException(json_last_error_msg());
}
if (!\is_array($content)) {
throw new \InvalidArgumentException('Invalid JSON data: expected array, got "%s"', gettype($content)));
}

How about this?

Copy link
Member Author

@aschempp aschempp Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JsonException is only available from PHP 7.4 😉
and with 7.4 we could just tell JSON to throw that exception

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we could, but why would we need different exceptions at all? 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using the polyfill already since #63


return $content;
}
}