Skip to content

Throw more specific exceptions in JsonPatch::import() #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/JsonPatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ public static function import(array $data)
$operation = (object)$operation;
}

if (!is_object($operation)) {
throw new Exception( 'Invalid patch operation - should be a JSON object' );
}

if (!isset($operation->op)) {
throw new Exception('Missing "op" in operation data');
throw new MissingFieldException('op', $operation);
}
if (!isset($operation->path)) {
throw new Exception('Missing "path" in operation data');
throw new MissingFieldException('path', $operation);
}

$op = null;
Expand All @@ -88,18 +92,18 @@ public static function import(array $data)
$op = new Test();
break;
default:
throw new Exception('Unknown "op": ' . $operation->op);
throw new UnknownOperationException($operation);
}
$op->path = $operation->path;
if ($op instanceof OpPathValue) {
if (property_exists($operation, 'value')) {
$op->value = $operation->value;
} else {
throw new Exception('Missing "value" in operation data');
throw new MissingFieldException('value', $operation);
}
} elseif ($op instanceof OpPathFrom) {
if (!isset($operation->from)) {
throw new Exception('Missing "from" in operation data');
throw new MissingFieldException('from', $operation);
}
$op->from = $operation->from;
}
Expand Down
47 changes: 47 additions & 0 deletions src/MissingFieldException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Swaggest\JsonDiff;

use Throwable;

class MissingFieldException extends Exception
{
/** @var string */
private $missingField;
/** @var object */
private $operation;

/**
* @param string $missingField
* @param object $operation
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
$missingField,
$operation,
$code = 0,
Throwable $previous = null
)
{
parent::__construct('Missing "' . $missingField . '" in operation data', $code, $previous);
$this->missingField = $missingField;
$this->operation = $operation;
}

/**
* @return string
*/
public function getMissingField()
{
return $this->missingField;
}

/**
* @return object
*/
public function getOperation()
{
return $this->operation;
}
}
36 changes: 36 additions & 0 deletions src/UnknownOperationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Swaggest\JsonDiff;


use Throwable;

class UnknownOperationException extends Exception
{
/** @var object */
private $operation;

/**
* @param object $operation
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
$operation,
$code = 0,
Throwable $previous = null
)
{
// @phpstan-ignore-next-line MissingFieldOperation will be thrown if op is not set
parent::__construct('Unknown "op": ' . $operation->op, $code, $previous);
$this->operation = $operation;
}

/**
* @return object
*/
public function getOperation()
{
return $this->operation;
}
}
6 changes: 4 additions & 2 deletions tests/src/JsonPatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Swaggest\JsonDiff\Exception;
use Swaggest\JsonDiff\JsonDiff;
use Swaggest\JsonDiff\JsonPatch;
use Swaggest\JsonDiff\MissingFieldException;
use Swaggest\JsonDiff\PatchTestOperationFailedException;
use Swaggest\JsonDiff\UnknownOperationException;

class JsonPatchTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -75,7 +77,7 @@ public function testNull()

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

Expand All @@ -87,7 +89,7 @@ public function testMissingPath()

public function testInvalidOp()
{
$this->setExpectedException(get_class(new Exception()), 'Unknown "op": wat');
$this->setExpectedException(UnknownOperationException::class, 'Unknown "op": wat');
JsonPatch::import(array((object)array('op' => 'wat', 'path' => '/123')));
}

Expand Down