-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
1404c36
ef32d3c
a329358
30c80cb
0d9f16f
ad8a50f
31b94b0
0c380b6
3d2e7ff
0574236
ebf1ece
e50b0f4
76fd6cb
8e87d01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
namespace Symfony\Component\Serializer\Encoder; | ||
|
||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
|
||
/** | ||
* Defines the interface of encoders | ||
* | ||
|
@@ -26,6 +28,8 @@ interface EncoderInterface | |
* @param array $context options that normalizers/encoders have access to. | ||
* | ||
* @return scalar | ||
* | ||
* @throws UnexpectedValueException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't you need also here the use statement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Sorry guys, first code PR, still learning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doing great! ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am learning too 👶 |
||
*/ | ||
public function encode($data, $format, array $context = array()); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
namespace Symfony\Component\Serializer\Encoder; | ||
|
||
use Symfony\Component\Serializer\Exception\UnexpectedValueException; | ||
|
||
/** | ||
* Decodes JSON data | ||
* | ||
|
@@ -33,6 +35,7 @@ class JsonDecode implements DecoderInterface | |
private $recursionDepth; | ||
|
||
private $lastError = JSON_ERROR_NONE; | ||
|
||
protected $serializer; | ||
|
||
/** | ||
|
@@ -52,6 +55,7 @@ public function __construct($associative = false, $depth = 512) | |
* | ||
* @return integer | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would keep it and mark it as deprecated. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
@@ -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()) | ||
|
@@ -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; | ||
} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated