Skip to content

Commit

Permalink
Merge pull request #57 from wmde/master
Browse files Browse the repository at this point in the history
Improve JsonPatch exceptions
vearutop authored Oct 21, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents ccfc713 + 478e618 commit 4eb3442
Showing 9 changed files with 344 additions and 34 deletions.
37 changes: 29 additions & 8 deletions src/JsonPatch.php
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
}
@@ -141,20 +145,26 @@ public function apply(&$original, $stopOnError = true)
$errors = array();
foreach ($this->operations as $operation) {
try {
// track the current pointer field so we can use it for a potential PathException
$pointerField = 'path';
$pathItems = JsonPointer::splitPath($operation->path);
switch (true) {
case $operation instanceof Add:
JsonPointer::add($original, $pathItems, $operation->value, $this->flags);
break;
case $operation instanceof Copy:
$pointerField = 'from';
$fromItems = JsonPointer::splitPath($operation->from);
$value = JsonPointer::get($original, $fromItems);
$pointerField = 'path';
JsonPointer::add($original, $pathItems, $value, $this->flags);
break;
case $operation instanceof Move:
$pointerField = 'from';
$fromItems = JsonPointer::splitPath($operation->from);
$value = JsonPointer::get($original, $fromItems);
JsonPointer::remove($original, $fromItems, $this->flags);
$pointerField = 'path';
JsonPointer::add($original, $pathItems, $value, $this->flags);
break;
case $operation instanceof Remove:
@@ -170,11 +180,22 @@ public function apply(&$original, $stopOnError = true)
$diff = new JsonDiff($operation->value, $value,
JsonDiff::STOP_ON_DIFF);
if ($diff->getDiffCnt() !== 0) {
throw new PatchTestOperationFailedException('Test operation ' . json_encode($operation, JSON_UNESCAPED_SLASHES)
. ' failed: ' . json_encode($value));
throw new PatchTestOperationFailedException($operation, $value);
}
break;
}
} catch (JsonPointerException $jsonPointerException) {
$pathException = new PathException(
$jsonPointerException->getMessage(),
$operation,
$pointerField,
$jsonPointerException->getCode()
);
if ($stopOnError) {
throw $pathException;
} else {
$errors[] = $pathException;
}
} catch (Exception $exception) {
if ($stopOnError) {
throw $exception;
@@ -185,4 +206,4 @@ public function apply(&$original, $stopOnError = true)
}
return $errors;
}
}
}
30 changes: 15 additions & 15 deletions src/JsonPointer.php
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ public static function splitPath($path)
return self::splitPathURIFragment($pathItems);
} else {
if ($first !== '') {
throw new Exception('Path must start with "/": ' . $path);
throw new JsonPointerException('Path must start with "/": ' . $path);
}
return self::splitPathJsonString($pathItems);
}
@@ -105,15 +105,15 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
while (null !== $key = array_shift($pathItems)) {
if ($ref instanceof \stdClass || is_object($ref)) {
if (PHP_VERSION_ID < 70100 && '' === $key) {
throw new Exception('Empty property name is not supported by PHP <7.1',
throw new JsonPointerException('Empty property name is not supported by PHP <7.1',
Exception::EMPTY_PROPERTY_NAME_UNSUPPORTED);
}

if ($flags & self::RECURSIVE_KEY_CREATION) {
$ref = &$ref->$key;
} else {
if (!isset($ref->$key) && count($pathItems)) {
throw new Exception('Non-existent path item: ' . $key);
throw new JsonPointerException('Non-existent path item: ' . $key);
} else {
$ref = &$ref->$key;
}
@@ -126,7 +126,7 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
$ref = new \stdClass();
$ref = &$ref->{$key};
} else {
throw new Exception('Non-existent path item: ' . $key);
throw new JsonPointerException('Non-existent path item: ' . $key);
}
} elseif ([] === $ref && 0 === ($flags & self::STRICT_MODE) && false === $intKey && '-' !== $key) {
$ref = new \stdClass();
@@ -138,7 +138,7 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
} else {
if (false === $intKey) {
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
throw new Exception('Invalid key for array operation');
throw new JsonPointerException('Invalid key for array operation');
}
$ref = &$ref[$key];
continue;
@@ -148,9 +148,9 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
}
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
if ($intKey > count($ref) && 0 === ($flags & self::RECURSIVE_KEY_CREATION)) {
throw new Exception('Index is greater than number of items in array');
throw new JsonPointerException('Index is greater than number of items in array');
} elseif ($intKey < 0) {
throw new Exception('Negative index');
throw new JsonPointerException('Negative index');
}
}

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

$vars = (array)$ref;
if (self::arrayKeyExists($key, $vars)) {
$ref = self::arrayGet($key, $vars);
} else {
throw new Exception('Key not found: ' . $key);
throw new JsonPointerException('Key not found: ' . $key);
}
} elseif (is_array($ref)) {
if (self::arrayKeyExists($key, $ref)) {
$ref = $ref[$key];
} else {
throw new Exception('Key not found: ' . $key);
throw new JsonPointerException('Key not found: ' . $key);
}
} elseif (is_object($ref)) {
if (isset($ref->$key)) {
$ref = $ref->$key;
} else {
throw new Exception('Key not found: ' . $key);
throw new JsonPointerException('Key not found: ' . $key);
}
} else {
throw new Exception('Key not found: ' . $key);
throw new JsonPointerException('Key not found: ' . $key);
}
}
return $ref;
@@ -260,19 +260,19 @@ public static function remove(&$holder, $pathItems, $flags = 0)
if (property_exists($ref, $key)) {
$ref = &$ref->$key;
} else {
throw new Exception('Key not found: ' . $key);
throw new JsonPointerException('Key not found: ' . $key);
}
} elseif (is_object($ref)) {
if (isset($ref->$key)) {
$ref = &$ref->$key;
} else {
throw new Exception('Key not found: ' . $key);
throw new JsonPointerException('Key not found: ' . $key);
}
} else {
if (array_key_exists($key, $ref)) {
$ref = &$ref[$key];
} else {
throw new Exception('Key not found: ' . $key);
throw new JsonPointerException('Key not found: ' . $key);
}
}
}
5 changes: 5 additions & 0 deletions src/JsonPointerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Swaggest\JsonDiff;

class JsonPointerException extends Exception {}
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;
}
}
43 changes: 42 additions & 1 deletion src/PatchTestOperationFailedException.php
Original file line number Diff line number Diff line change
@@ -3,6 +3,47 @@
namespace Swaggest\JsonDiff;


use Throwable;

class PatchTestOperationFailedException extends Exception
{
}
/** @var object */
private $operation;
/** @var string */
private $actualValue;

/**
* @param object $operation
* @param string $actualValue
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
$operation,
$actualValue,
$code = 0,
Throwable $previous = null
)
{
parent::__construct('Test operation ' . json_encode($operation, JSON_UNESCAPED_SLASHES)
. ' failed: ' . json_encode($actualValue), $code, $previous);
$this->operation = $operation;
$this->actualValue = $actualValue;
}

/**
* @return object
*/
public function getOperation()
{
return $this->operation;
}

/**
* @return string
*/
public function getActualValue()
{
return $this->actualValue;
}
}
51 changes: 51 additions & 0 deletions src/PathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Swaggest\JsonDiff;


use Throwable;

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

/** @var string */
private $field;

/**
* @param string $message
* @param object $operation
* @param string $field
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
$message,
$operation,
$field,
$code = 0,
Throwable $previous = null
)
{
parent::__construct($message, $code, $previous);
$this->operation = $operation;
$this->field = $field;
}

/**
* @return object
*/
public function getOperation()
{
return $this->operation;
}

/**
* @return string
*/
public function getField()
{
return $this->field;
}
}
Loading

0 comments on commit 4eb3442

Please sign in to comment.