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
2 changes: 2 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ CHANGELOG
-----

* added `$context` support for XMLEncoder.
* [DEPRECATION] JsonEncode and JsonDecode where modified to throw
an exception if error found. No need for get*Error() functions

2.3.0
-----
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/DecoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* Defines the interface of decoders
*
Expand All @@ -31,6 +33,8 @@ interface DecoderInterface
* phpdoc comment.
*
* @return mixed
*
* @throws UnexpectedValueException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add the use statement as the exception is not in the same namespace

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

*/
public function decode($data, $format, array $context = array());

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/EncoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* Defines the interface of encoders
*
Expand All @@ -26,6 +28,8 @@ interface EncoderInterface
* @param array $context options that normalizers/encoders have access to.
*
* @return scalar
*
* @throws UnexpectedValueException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need also here the use statement?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Sorry guys, first code PR, still learning

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing great! ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am learning too 👶

*/
public function encode($data, $format, array $context = array());

Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* Decodes JSON data
*
Expand All @@ -33,6 +35,7 @@ class JsonDecode implements DecoderInterface
private $recursionDepth;

private $lastError = JSON_ERROR_NONE;

protected $serializer;

/**
Expand All @@ -52,6 +55,7 @@ public function __construct($associative = false, $depth = 512)
*
* @return integer
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing public methods is a BC break, you should revert this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep it and mark it as deprecated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

functions are now marked as deprecated. Serializer changelog updated too

* @deprecated decode() throws an exception if error found
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
*/
public function getLastError()
Expand Down Expand Up @@ -82,6 +86,8 @@ public function getLastError()
*
* @return mixed
*
* @throws UnexpectedValueException
*
* @see http://php.net/json_decode json_decode
*/
public function decode($data, $format, array $context = array())
Expand All @@ -98,7 +104,10 @@ public function decode($data, $format, array $context = array())
$decodedData = json_decode($data, $associative, $recursionDepth);
}

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

return $decodedData;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/**
* Encodes JSON data
*
Expand All @@ -27,10 +29,11 @@ public function __construct($bitmask = 0)
}

/**
* Returns the last encoding error (if any)
* Returns the last encoding error (if any).
*
* @return integer
*
* @deprecated encode() throws an exception if error found
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
*/
public function getLastError()
Expand All @@ -48,7 +51,11 @@ public function encode($data, $format, array $context = array())
$context = $this->resolveContext($context);

$encodedJson = json_encode($data, $context['json_encode_options']);
$this->lastError = json_last_error();

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

return $encodedJson;
}
Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,25 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin
}

/**
*
* Returns the last encoding error (if any)
*
* @return integer
*
* @deprecated JsonEncode throws exception if an error is found
*/
public function getLastEncodingError()
{
return $this->encodingImpl->getLastError();
}

/**
*
* Returns the last decoding error (if any)
*
* @return integer
*
* @deprecated JsonDecode throws exception if an error is found
*/
public function getLastDecodingError()
{
Expand Down Expand Up @@ -87,4 +93,31 @@ public function supportsDecoding($format)
{
return self::FORMAT === $format;
}

/**
* Resolves json_last_error message
*
* @return string
*/
public static function getLastErrorMessage()
{
if (function_exists('json_last_error_msg')) {
return json_last_error_msg();
}

switch (json_last_error()) {
case JSON_ERROR_DEPTH:
return 'Maximum stack depth exceeded';
case JSON_ERROR_STATE_MISMATCH:
return 'Underflow or the modes mismatch';
case JSON_ERROR_CTRL_CHAR:
return 'Unexpected control character found';
case JSON_ERROR_SYNTAX:
return 'Syntax error, malformed JSON';
case JSON_ERROR_UTF8:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
default:
return 'Unknown error';
}
}
}