Skip to content

Commit 8efb691

Browse files
Add operation index to PathException for detailed error reporting
Enhanced the `PathException` exception class to include an operation index, identifying the specific operation that caused the exception. Modified the `JsonPatch` class to pass this additional parameter when throwing the `PathException`. Updated the corresponding tests to reflect these changes.
1 parent 17bfc66 commit 8efb691

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/JsonPatch.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function jsonSerialize()
151151
public function apply(&$original, $stopOnError = true)
152152
{
153153
$errors = array();
154-
foreach ($this->operations as $operation) {
154+
foreach ($this->operations as $opIndex => $operation) {
155155
try {
156156
// track the current pointer field so we can use it for a potential PathException
157157
$pointerField = 'path';
@@ -197,7 +197,8 @@ public function apply(&$original, $stopOnError = true)
197197
$jsonPointerException->getMessage(),
198198
$operation,
199199
$pointerField,
200-
$jsonPointerException->getCode()
200+
$jsonPointerException->getCode(),
201+
$opIndex
201202
);
202203
if ($stopOnError) {
203204
throw $pathException;

src/PathException.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,30 @@ class PathException extends Exception
1414
/** @var string */
1515
private $field;
1616

17+
/** @var int */
18+
private $opIndex;
19+
1720
/**
1821
* @param string $message
1922
* @param OpPath $operation
2023
* @param string $field
2124
* @param int $code
25+
* @param int $opIndex
2226
* @param Throwable|null $previous
2327
*/
2428
public function __construct(
2529
$message,
2630
$operation,
2731
$field,
2832
$code = 0,
33+
$opIndex,
2934
Throwable $previous = null
3035
)
3136
{
3237
parent::__construct($message, $code, $previous);
3338
$this->operation = $operation;
3439
$this->field = $field;
40+
$this->opIndex = $opIndex;
3541
}
3642

3743
/**
@@ -49,4 +55,12 @@ public function getField()
4955
{
5056
return $this->field;
5157
}
58+
59+
/**
60+
* @return int
61+
*/
62+
public function getOpIndex(): int
63+
{
64+
return $this->opIndex;
65+
}
5266
}

tests/src/JsonPatchTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,21 @@ public function testPathExceptionContinueOnError()
229229
$data = array('abc' => $actualValue);
230230
$patch = new JsonPatch();
231231

232-
$operation1 = new JsonPatch\Test('/abc', 'def');
233-
$patch->op($operation1);
232+
$operation0 = new JsonPatch\Test('/abc', 'def');
233+
$patch->op($operation0);
234234

235-
$operation2 = new JsonPatch\Move('/target', '/source');
236-
$patch->op($operation2);
235+
$operation1 = new JsonPatch\Move('/target', '/source');
236+
$patch->op($operation1);
237237

238238
$errors = $patch->apply($data, false);
239239

240240
$this->assertInstanceOf(PatchTestOperationFailedException::class, $errors[0]);
241-
$this->assertSame($operation1, $errors[0]->getOperation());
241+
$this->assertSame($operation0, $errors[0]->getOperation());
242242

243243
$this->assertInstanceOf(PathException::class, $errors[1]);
244-
$this->assertSame($operation2, $errors[1]->getOperation());
244+
$this->assertSame($operation1, $errors[1]->getOperation());
245245
$this->assertSame('from', $errors[1]->getField());
246+
$this->assertEquals(1, $errors[1]->getOpIndex());
246247
}
247248

248249
public function pathExceptionProvider() {
@@ -305,6 +306,7 @@ public function testPathException(OpPath $operation, $expectedMessage, $expected
305306
$this->assertInstanceOf(PathException::class, $ex);
306307
$this->assertEquals($expectedMessage, $ex->getMessage());
307308
$this->assertEquals($expectedField, $ex->getField());
309+
$this->assertEquals(0, $ex->getOpIndex()); // There is only one operation
308310
}
309311
}
310312
}

0 commit comments

Comments
 (0)