Skip to content

Commit 48fb056

Browse files
authored
Include input string in message about invalid json (#711)
1 parent cbd07bc commit 48fb056

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88

99
## [Unreleased](https://github.com/algolia/algoliasearch-client-php/compare/3.3.2...master)
1010

11+
### Added
12+
13+
- Include input string in message about invalid json ([#711](https://github.com/algolia/algoliasearch-client-php/pull/711))
14+
1115
## [v3.3.2](https://github.com/algolia/algoliasearch-client-php/compare/3.3.1...3.3.2)
1216

1317
### Fixed

src/Support/Helpers.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,15 @@ public static function json_decode($json, $assoc = false, $depth = 512)
109109
{
110110
$data = \json_decode($json, $assoc, $depth);
111111
if (JSON_ERROR_NONE !== json_last_error()) {
112-
throw new \InvalidArgumentException('json_decode error: '.json_last_error_msg());
112+
throw new \InvalidArgumentException(sprintf(
113+
<<<'EXCEPTION'
114+
json_decode_error: %s
115+
input string: %s
116+
EXCEPTION
117+
,
118+
json_last_error_msg(),
119+
$json
120+
));
113121
}
114122

115123
return $data;

tests/Unit/HelpersTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,12 @@ public function testMapObjectIDsWithMissingPrimary()
7979
$objects = [['name' => 'test'], ['primary' => 1, 'name' => 'cool']];
8080
Helpers::mapObjectIDs('primary', $objects);
8181
}
82+
83+
public function testItMentionsTheInputStringWhenItIsInvalid(): void
84+
{
85+
$this->expectException(\InvalidArgumentException::class);
86+
$this->expectExceptionMessage('this is not valid json');
87+
88+
Helpers::json_decode('this is not valid json');
89+
}
8290
}

0 commit comments

Comments
 (0)