Skip to content

Commit d363405

Browse files
dpritchardOriHoch
authored andcommitted
More helpful exceptions when JSON is invalid (#38)
* More helpful exceptions when JSON is invalid * Cleared against `composer style-fix` locally
1 parent 3c3b412 commit d363405

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/Factory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ protected static function loadSource($source, $basePath)
312312
}
313313
}
314314
}
315+
if (json_last_error()) {
316+
throw new Exceptions\DatapackageInvalidSourceException(
317+
json_last_error_msg().' when loading source: '.json_encode($source)
318+
);
319+
}
315320
} else {
316321
throw new Exceptions\DatapackageInvalidSourceException(
317322
'Invalid source: '.json_encode($source)

tests/DatapackageTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ function () { Package::load('-invalid-'); }
9696
);
9797
}
9898

99+
public function testJsonInvalidSyntaxShouldFail()
100+
{
101+
// http://php.net/manual/en/function.json-decode.php
102+
// trailing commas are not allowed
103+
$bad_json = '{ "bar": "baz", }';
104+
json_decode($bad_json); // null
105+
$this->assertDatapackageException(
106+
'frictionlessdata\\datapackage\\Exceptions\\DatapackageInvalidSourceException',
107+
json_last_error_msg().' when loading source: '.json_encode($bad_json),
108+
function () use ($bad_json) { Package::load($bad_json); }
109+
);
110+
}
111+
99112
public function testJsonFileRelativeToBasePath()
100113
{
101114
$this->assertDatapackage(

0 commit comments

Comments
 (0)