10000 [Serializer] error handling inconsistencies fixed in the serializer decoders by rodrigodiez · Pull Request #9586 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] error handling inconsistencies fixed in the serializer decoders #9586

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

Closed
wants to merge 14 commits into from
Closed
Prev Previous commit
Next Next commit
BC break reverted
  • Loading branch information
Rodrigo Díez Villamuera committed Nov 23, 2013
commit ad8a50fb0d358824901c00435eb401b6152180d4
18 changes: 16 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class JsonDecode implements DecoderInterface
*/
private $recursionDepth;

private $lastError = JSON_ERROR_NONE;

protected $serializer;

/**
Expand All @@ -48,6 +50,18 @@ public function __construct($associative = false, $depth = 512)
$this->recursionDepth = (int) $depth;
}

/**
* Returns the last decoding error (if any).
*
* @return integer
*
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
*/
public function getLastError()
{
return $this->lastError;
}

/**
* Decodes data.
*
Expand Down Expand Up @@ -89,8 +103,8 @@ public function decode($data, $format, array $context = array())
$decodedData = json_decode($data, $associative, $recursionDepth);
}

if (JSON_ERROR_NONE !== $error = json_last_error()) {
$message = JsonEncoder::getLastErrorMessage();
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
$message = JsonEncoder::getErrorMessage($this->lastError);
throw new UnexpectedValueException($message);
}

Expand Down
15 changes: 14 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@
class JsonEncode implements EncoderInterface
{
private $options ;
private $lastError = JSON_ERROR_NONE;

public function __construct($bitmask = 0)
{
$this->options = $bitmask;
}

/**
* Returns the last encoding error (if any).
*
* @return integer
*
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
*/
public function getLastError()
{
return $this->lastError;
}

/**
* Encodes PHP data to a JSON string
*
Expand All @@ -38,7 +51,7 @@ public function encode($data, $format, array $context = array())

$encodedJson = json_encode($data, $context['json_encode_options']);

if (JSON_ERROR_NONE !== $error = json_last_error()) {
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
$message = JsonEncoder::getLastErrorMessage();
throw new UnexpectedValueException($message);
}
Expand Down
67 changes: 42 additions & 25 deletions src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin
$this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
}

/**
* Returns the last encoding error (if any)
*
* @return integer
*/
public function getLastEncodingError()
{
return $this->encodingImpl->getLastError();
}

/**
* Returns the last decoding error (if any)
*
* @return integer
*/
public function getLastDecodingError()
{
return $this->decodingImpl->getLastError();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -71,34 +91,31 @@ public function supportsDecoding($format)
/**
* Resolves json_last_error message
*
* @param $error
*
* @return string
*/
public static function getLastErrorMessage()
public static function getErrorMessage($error)
{
// PHP 5 < 5.5.0
if (!function_exists('json_last_error_msg')) {
switch (json_last_error()) {
default:
return 'Unknown error';
break;
case JSON_ERROR_DEPTH:
return 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
return 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
return 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
return 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
}
switch ($error) {
default:
return 'Unknown error';
break;
case JSON_ERROR_DEPTH:
return 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
return 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
return 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
return 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
}

return json_last_error_msg();
}
}
0