8000 [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
Next Next commit
Json encoder classes now throws UnexpectedValueException as XML classes
  • Loading branch information
Rodrigo Díez Villamuera committed Nov 23, 2013
commit 1404c366b6396a43e5516d34f756ae9bda1645eb
2 changes: 2 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/DecoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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
2 changes: 2 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/EncoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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
21 changes: 8 additions & 13 deletions 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 Down Expand Up @@ -47,18 +49,6 @@ 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 All @@ -82,6 +72,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 +90,10 @@ public function decode($data, $format, array $context = array())
$decodedData = json_decode($data, $associative, $recursionDepth);
}

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

return $decodedData;
}
Expand Down
8 changes: 7 additions & 1 deletion 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 Down Expand Up @@ -48,7 +50,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 !== $error = json_last_error()) {
$message = JsonEncoder::getErrorMessage($error);
throw new UnexpectedValueException($message);
}

return $encodedJson;
}
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,41 @@ public function supportsDecoding($format)
{
return self::FORMAT === $format;
}

/**
* Resolves json_last_error message
*
* @param $error
*
* @return string
*/
public static function getErrorMessage($error)
{
if (!function_exists('json_last_error_msg')) {
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 even reduce the indentation of the whole method code, by switching the condition:

if (function_exists('json_last_error_msg')) {
    return json_last_error_msg();
}

switch (json_last_error()) {
    // ...
}

Copy link
Author

Choose a reason for hiding this comment

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

I totally changed this function, preserving only error message resolution based on a error parameter, no matter if last or not. It can be usefull for PHP versions < 5.5.0. What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

No sense. Updated

switch (json_last_error()) {
default:
Copy link
Member

Choose a reason for hiding this comment

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

default should come last

Copy link
Author

Choose a reason for hiding this comment

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

updated

$message = 'Unknown error';
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using variable, you can use return:

if (!function_exists('json_last_error_msg')) {
    switch (json_last_error()) {
        default:
            return 'Unknown error';
    }
}

return json_last_error_msg();

Copy link
Author

Choose a reason for hiding this comment

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

updated

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, default should probably be the last case.

Copy link
Author

Choose a reason for hiding this comment

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

updated

break;
Copy link
Contributor

Choose a reason for hiding this comment

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

As you call return, the break is redundant here.

Copy link
Author

Choose a reason for hiding this comment

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

updated

case JSON_ERROR_DEPTH:
$message = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$message = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$message = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$message = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$message = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
}
} else {
$message = json_last_error_msg();
}

return $error;
}
}
0