Skip to content

Commit eae1256

Browse files
Silvan-WMDEjakobw
authored andcommitted
Introduce JsonPointerException
Replace all occurences of the generic Exception in JsonPointer with the more specific JsonPointerException so that the origin of the error is made explicit and can be handled by consuming code.
1 parent 5f297cb commit eae1256

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/JsonPointer.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static function splitPath($path)
6666
return self::splitPathURIFragment($pathItems);
6767
} else {
6868
if ($first !== '') {
69-
throw new Exception('Path must start with "/": ' . $path);
69+
throw new JsonPointerException('Path must start with "/": ' . $path);
7070
}
7171
return self::splitPathJsonString($pathItems);
7272
}
@@ -105,15 +105,15 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
105105
while (null !== $key = array_shift($pathItems)) {
106106
if ($ref instanceof \stdClass || is_object($ref)) {
107107
if (PHP_VERSION_ID < 70100 && '' === $key) {
108-
throw new Exception('Empty property name is not supported by PHP <7.1',
108+
throw new JsonPointerException('Empty property name is not supported by PHP <7.1',
109109
Exception::EMPTY_PROPERTY_NAME_UNSUPPORTED);
110110
}
111111

112112
if ($flags & self::RECURSIVE_KEY_CREATION) {
113113
$ref = &$ref->$key;
114114
} else {
115115
if (!isset($ref->$key) && count($pathItems)) {
116-
throw new Exception('Non-existent path item: ' . $key);
116+
throw new JsonPointerException('Non-existent path item: ' . $key);
117117
} else {
118118
$ref = &$ref->$key;
119119
}
@@ -126,7 +126,7 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
126126
$ref = new \stdClass();
127127
$ref = &$ref->{$key};
128128
} else {
129-
throw new Exception('Non-existent path item: ' . $key);
129+
throw new JsonPointerException('Non-existent path item: ' . $key);
130130
}
131131
} elseif ([] === $ref && 0 === ($flags & self::STRICT_MODE) && false === $intKey && '-' !== $key) {
132132
$ref = new \stdClass();
@@ -138,7 +138,7 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
138138
} else {
139139
if (false === $intKey) {
140140
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
141-
throw new Exception('Invalid key for array operation');
141+
throw new JsonPointerException('Invalid key for array operation');
142142
}
143143
$ref = &$ref[$key];
144144
continue;
@@ -148,9 +148,9 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
148148
}
149149
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
150150
if ($intKey > count($ref) && 0 === ($flags & self::RECURSIVE_KEY_CREATION)) {
151-
throw new Exception('Index is greater than number of items in array');
151+
throw new JsonPointerException('Index is greater than number of items in array');
152152
} elseif ($intKey < 0) {
153-
throw new Exception('Negative index');
153+
throw new JsonPointerException('Negative index');
154154
}
155155
}
156156

@@ -203,30 +203,30 @@ public static function get($holder, $pathItems)
203203
while (null !== $key = array_shift($pathItems)) {
204204
if ($ref instanceof \stdClass) {
205205
if (PHP_VERSION_ID < 70100 && '' === $key) {
206-
throw new Exception('Empty property name is not supported by PHP <7.1',
206+
throw new JsonPointerException('Empty property name is not supported by PHP <7.1',
207207
Exception::EMPTY_PROPERTY_NAME_UNSUPPORTED);
208208
}
209209

210210
$vars = (array)$ref;
211211
if (self::arrayKeyExists($key, $vars)) {
212212
$ref = self::arrayGet($key, $vars);
213213
} else {
214-
throw new Exception('Key not found: ' . $key);
214+
throw new JsonPointerException('Key not found: ' . $key);
215215
}
216216
} elseif (is_array($ref)) {
217217
if (self::arrayKeyExists($key, $ref)) {
218218
$ref = $ref[$key];
219219
} else {
220-
throw new Exception('Key not found: ' . $key);
220+
throw new JsonPointerException('Key not found: ' . $key);
221221
}
222222
} elseif (is_object($ref)) {
223223
if (isset($ref->$key)) {
224224
$ref = $ref->$key;
225225
} else {
226-
throw new Exception('Key not found: ' . $key);
226+
throw new JsonPointerException('Key not found: ' . $key);
227227
}
228228
} else {
229-
throw new Exception('Key not found: ' . $key);
229+
throw new JsonPointerException('Key not found: ' . $key);
230230
}
231231
}
232232
return $ref;
@@ -260,19 +260,19 @@ public static function remove(&$holder, $pathItems, $flags = 0)
260260
if (property_exists($ref, $key)) {
261261
$ref = &$ref->$key;
262262
} else {
263-
throw new Exception('Key not found: ' . $key);
263+
throw new JsonPointerException('Key not found: ' . $key);
264264
}
265265
} elseif (is_object($ref)) {
266266
if (isset($ref->$key)) {
267267
$ref = &$ref->$key;
268268
} else {
269-
throw new Exception('Key not found: ' . $key);
269+
throw new JsonPointerException('Key not found: ' . $key);
270270
}
271271
} else {
272272
if (array_key_exists($key, $ref)) {
273273
$ref = &$ref[$key];
274274
} else {
275-
throw new Exception('Key not found: ' . $key);
275+
throw new JsonPointerException('Key not found: ' . $key);
276276
}
277277
}
278278
}

src/JsonPointerException.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Swaggest\JsonDiff;
4+
5+
class JsonPointerException extends Exception {}

tests/src/JsonPointerTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Swaggest\JsonDiff\Exception;
77
use Swaggest\JsonDiff\JsonPointer;
8+
use Swaggest\JsonDiff\JsonPointerException;
89

910
class JsonPointerTest extends \PHPUnit_Framework_TestCase
1011
{
@@ -30,7 +31,7 @@ public function testProcess()
3031

3132
try {
3233
$this->assertSame('null', json_encode(JsonPointer::get($json, JsonPointer::splitPath('/l1/l2/non-existent'))));
33-
} catch (Exception $exception) {
34+
} catch (JsonPointerException $exception) {
3435
$this->assertSame('Key not found: non-existent', $exception->getMessage());
3536
}
3637

@@ -89,7 +90,7 @@ public function testGetSetDeleteObject()
8990
try {
9091
JsonPointer::get($s, ['one', 'two']);
9192
$this->fail('Exception expected');
92-
} catch (Exception $e) {
93+
} catch (JsonPointerException $e) {
9394
$this->assertEquals('Key not found: two', $e->getMessage());
9495
}
9596
$this->assertEquals(null, $s->one->two);

0 commit comments

Comments
 (0)