Skip to content

Commit

Permalink
Add operation index to PathException for detailed error reporting
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MuhammadJaziraly committed Jul 2, 2024
1 parent 17bfc66 commit 27f5d03
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/JsonPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function jsonSerialize()
public function apply(&$original, $stopOnError = true)
{
$errors = array();
foreach ($this->operations as $operation) {
foreach ($this->operations as $opIndex => $operation) {
try {
// track the current pointer field so we can use it for a potential PathException
$pointerField = 'path';
Expand Down Expand Up @@ -199,6 +199,7 @@ public function apply(&$original, $stopOnError = true)
$pointerField,
$jsonPointerException->getCode()
);
$pathException->setOpIndex($opIndex);
if ($stopOnError) {
throw $pathException;
} else {
Expand Down
21 changes: 21 additions & 0 deletions src/PathException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class PathException extends Exception
/** @var string */
private $field;

/** @var int */
private $opIndex;

/**
* @param string $message
* @param OpPath $operation
Expand Down Expand Up @@ -49,4 +52,22 @@ public function getField()
{
return $this->field;
}

/**
* @param int $opIndex
* @return $this
*/
public function setOpIndex($opIndex)
{
$this->opIndex = $opIndex;
return $this;
}

/**
* @return int
*/
public function getOpIndex()
{
return $this->opIndex;
}
}
14 changes: 8 additions & 6 deletions tests/src/JsonPatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,21 @@ public function testPathExceptionContinueOnError()
$data = array('abc' => $actualValue);
$patch = new JsonPatch();

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

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

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

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

$this->assertInstanceOf(PathException::class, $errors[1]);
$this->assertSame($operation2, $errors[1]->getOperation());
$this->assertSame($operation1, $errors[1]->getOperation());
$this->assertSame('from', $errors[1]->getField());
$this->assertEquals(1, $errors[1]->getOpIndex());
}

public function pathExceptionProvider() {
Expand Down Expand Up @@ -305,6 +306,7 @@ public function testPathException(OpPath $operation, $expectedMessage, $expected
$this->assertInstanceOf(PathException::class, $ex);
$this->assertEquals($expectedMessage, $ex->getMessage());
$this->assertEquals($expectedField, $ex->getField());
$this->assertEquals(0, $ex->getOpIndex()); // There is only one operation
}
}
}

0 comments on commit 27f5d03

Please sign in to comment.