Skip to content

Commit

Permalink
better error handling when decoding JSON dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
mledoze committed Sep 11, 2023
1 parent d7827fe commit 8fec221
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
13 changes: 7 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/MLD/Console/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MLD\Console\Command;

use JsonException;
use MLD\Converter\Factory;
use MLD\Enum\ExportCommandOptions;
use MLD\Enum\Formats;
Expand Down Expand Up @@ -90,7 +91,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->setOutputDirectory($input);
$this->createOutputDirectory($output);

$countries = $this->decodeInputFile();
try {
$countries = $this->decodeInputFile();
} catch (JsonException $exception) {
$output->writeln(sprintf('Failed to decode input countries file: %s', $exception->getMessage()));
return 1;
}

$outputFields = $this->getOutputFields($input, $countries);
if ($output->isVerbose()) {
Expand Down Expand Up @@ -194,10 +200,11 @@ private function getOutputFields(InputInterface $input, array $countries): array

/**
* @return array
* @throws JsonException
*/
private function decodeInputFile(): array
{
return json_decode(file_get_contents($this->inputFile), true);
return json_decode(file_get_contents($this->inputFile), true, 512, JSON_THROW_ON_ERROR);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/MLD/Converter/AbstractJsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract protected function jsonEncode(array $countries): string;
protected function processEmptyArrays(array $countries): array
{
return array_map(
function ($country) {
static function ($country) {
if (isset($country[Fields::LANGUAGES]) && empty($country[Fields::LANGUAGES])) {
$country[Fields::LANGUAGES] = new stdClass();
}
Expand Down
5 changes: 4 additions & 1 deletion src/MLD/Converter/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

namespace MLD\Converter;

use JsonException;

/**
* Convert countries data to JSON format
*/
class JsonConverter extends AbstractJsonConverter
{
/**
* @inheritDoc
* @throws JsonException
*/
protected function jsonEncode(array $countries): string
{
return json_encode($countries);
return json_encode($countries, JSON_THROW_ON_ERROR);
}
}
5 changes: 4 additions & 1 deletion src/MLD/Converter/JsonConverterUnicode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

namespace MLD\Converter;

use JsonException;

/**
* Convert countries data to JSON format with unescaped characters
*/
class JsonConverterUnicode extends AbstractJsonConverter
{
/**
* @inheritdoc
* @throws JsonException
*/
protected function jsonEncode(array $countries): string
{
return json_encode($countries, JSON_UNESCAPED_UNICODE);
return json_encode($countries, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE);
}
}
2 changes: 1 addition & 1 deletion src/MLD/Converter/XmlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private function processCountry(array $country): void
{
$countryNode = $this->domDocument->createElement('country');
$country = $this->flatten($country);
array_walk($country, function ($value, $key) use ($countryNode) {
array_walk($country, static function ($value, $key) use ($countryNode) {
$countryNode->setAttribute($key, (string) $value);
});
$this->domDocument->documentElement->appendChild($countryNode);
Expand Down
4 changes: 1 addition & 3 deletions src/MLD/Converter/YamlConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class YamlConverter extends AbstractConverter
*/
public function convert(array $countries): string
{
$dumper = new Dumper();

return $dumper->dump($countries, self::INLINE_LEVEL);
return (new Dumper())->dump($countries, self::INLINE_LEVEL);
}
}

0 comments on commit 8fec221

Please sign in to comment.