Skip to content

Commit

Permalink
move to json exception
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Feb 2, 2022
1 parent f8f938a commit 3f37c3d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/Driver/Traits/JsonMappingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Jgut\Mapping\Driver\Traits;

use Jgut\Mapping\Exception\DriverException;
use JsonException;

trait JsonMappingTrait
{
Expand All @@ -32,11 +33,18 @@ protected function getExtensions(): array
*/
protected function loadMappingFile(string $mappingFile): array
{
$mappingData = json_decode(file_get_contents($mappingFile), true, 512, \JSON_BIGINT_AS_STRING);

if (json_last_error() !== \JSON_ERROR_NONE) {
try {
$mappingData = json_decode(
file_get_contents($mappingFile),
true,
512,
\JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR,
);
} catch (JsonException $exception) {
throw new DriverException(
sprintf('JSON mapping file "%s" parsing error: %s.', $mappingFile, rtrim(json_last_error_msg(), '.')),
sprintf('JSON mapping file "%s" parsing error.', $mappingFile),
0,
$exception,
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Mapping/Driver/Traits/JsonMappingTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testExtensions(): void
public function testLoadError(): void
{
$this->expectException(DriverException::class);
$this->expectExceptionMessageMatches('/^JSON mapping file ".+" parsing error: Syntax error\.$/');
$this->expectExceptionMessageMatches('/^JSON mapping file ".+" parsing error\.$/');

$this->mapping->loadMappingFile(__DIR__ . '/../../Files/files/invalid/invalid.json');
}
Expand Down

0 comments on commit 3f37c3d

Please sign in to comment.