Skip to content

Commit cf5bac4

Browse files
committed
Throw more specific exceptions in JsonPatch::import()
- InvalidOperationException - MissingFieldException
1 parent ff3a792 commit cf5bac4

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

src/InvalidOperationException.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Swaggest\JsonDiff;
4+
5+
6+
use Throwable;
7+
8+
class InvalidOperationException extends Exception
9+
{
10+
private object $operationObject;
11+
12+
public function __construct(
13+
object $operationObject,
14+
string $message = '',
15+
int $code = 0,
16+
Throwable $previous = null
17+
)
18+
{
19+
parent::__construct($message, $code, $previous);
20+
$this->operationObject = $operationObject;
21+
}
22+
23+
public function getInvalidOperation(): string
24+
{
25+
return $this->operationObject->op;
26+
}
27+
28+
public function getOperationObject(): object
29+
{
30+
return $this->operationObject;
31+
}
32+
}

src/JsonPatch.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ public static function import(array $data)
6060
$operation = (object)$operation;
6161
}
6262

63+
if (!is_object($operation)) {
64+
throw new Exception( 'Invalid patch operation - should be a JSON object' );
65+
}
66+
6367
if (!isset($operation->op)) {
64-
throw new Exception('Missing "op" in operation data');
68+
throw new MissingFieldException('op', $operation, 'Missing "op" in operation data');
6569
}
6670
if (!isset($operation->path)) {
67-
throw new Exception('Missing "path" in operation data');
71+
throw new MissingFieldException('path', $operation, 'Missing "path" in operation data');
6872
}
6973

7074
$op = null;
@@ -88,18 +92,18 @@ public static function import(array $data)
8892
$op = new Test();
8993
break;
9094
default:
91-
throw new Exception('Unknown "op": ' . $operation->op);
95+
throw new InvalidOperationException($operation, 'Invalid "op": ' . $operation->op);
9296
}
9397
$op->path = $operation->path;
9498
if ($op instanceof OpPathValue) {
9599
if (property_exists($operation, 'value')) {
96100
$op->value = $operation->value;
97101
} else {
98-
throw new Exception('Missing "value" in operation data');
102+
throw new MissingFieldException('value', $operation, 'Missing "value" in operation data');
99103
}
100104
} elseif ($op instanceof OpPathFrom) {
101105
if (!isset($operation->from)) {
102-
throw new Exception('Missing "from" in operation data');
106+
throw new MissingFieldException('from', $operation, 'Missing "from" in operation data');
103107
}
104108
$op->from = $operation->from;
105109
}

src/MissingFieldException.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Swaggest\JsonDiff;
4+
5+
use Throwable;
6+
7+
class MissingFieldException extends Exception
8+
{
9+
private string $missingField;
10+
private object $operationObject;
11+
12+
public function __construct(
13+
string $missingField,
14+
object $operationObject,
15+
string $message = '',
16+
int $code = 0,
17+
Throwable $previous = null
18+
)
19+
{
20+
parent::__construct($message, $code, $previous);
21+
$this->missingField = $missingField;
22+
$this->operationObject = $operationObject;
23+
}
24+
25+
public function getMissingField(): string
26+
{
27+
return $this->missingField;
28+
}
29+
30+
public function getOperationObject(): object
31+
{
32+
return $this->operationObject;
33+
}
34+
}

tests/src/JsonPatchTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Swaggest\JsonDiff\Exception;
66
use Swaggest\JsonDiff\JsonDiff;
77
use Swaggest\JsonDiff\JsonPatch;
8+
use Swaggest\JsonDiff\MissingFieldException;
89
use Swaggest\JsonDiff\PatchTestOperationFailedException;
910

1011
class JsonPatchTest extends \PHPUnit_Framework_TestCase
@@ -75,7 +76,7 @@ public function testNull()
7576

7677
public function testMissingOp()
7778
{
78-
$this->setExpectedException(get_class(new Exception()), 'Missing "op" in operation data');
79+
$this->setExpectedException(MissingFieldException::class, 'Missing "op" in operation data');
7980
JsonPatch::import(array((object)array('path' => '/123')));
8081
}
8182

@@ -87,7 +88,7 @@ public function testMissingPath()
8788

8889
public function testInvalidOp()
8990
{
90-
$this->setExpectedException(get_class(new Exception()), 'Unknown "op": wat');
91+
$this->setExpectedException(get_class(new Exception()), 'Invalid "op": wat');
9192
JsonPatch::import(array((object)array('op' => 'wat', 'path' => '/123')));
9293
}
9394

0 commit comments

Comments
 (0)