Skip to content

[JsonResponse] Silent only JSON errors #11418

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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Symfony/Component/HttpFoundation/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,29 @@ public function setCallback($callback = null)
*/
public function setData($data = array())
{
$this->data = @json_encode($data, $this->encodingOptions);
$errorHandler = null;
$errorHandler = set_error_handler(function () use (&$errorHandler) {
if (JSON_ERROR_NONE !== json_last_error()) {
Copy link
Member

Choose a reason for hiding this comment

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

are you sure this will cover the case properly ? If the error happens early in the error handling, the json_last_error might be related to a previous json operation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, that's the tricky part.

I can run json_encode(null) before to clear json_last_error().
But it's still possible to execute json_encode or json_decode inside a JsonSerializable object.

Copy link
Member Author

Choose a reason for hiding this comment

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

The most reliable way to know if the error has been triggered by this call to json_encode is to check the $errfile and $errline.

return;
}

if ($errorHandler) {
call_user_func_array($errorHandler, func_get_args());
}
});

try {
// Clear json_last_error()
json_encode(null);

$this->data = json_encode($data, $this->encodingOptions);

restore_error_handler();
} catch (\Exception $exception) {
restore_error_handler();

throw $exception;
}

if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException($this->transformJsonError());
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,32 @@ public function testSetContent()
{
JsonResponse::create("\xB1\x31");
}

/**
* @expectedException Exception
Copy link
Member Author

Choose a reason for hiding this comment

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

The warning is converted to an exception by PHPUnit.

* @expectedExceptionMessage Failed calling Symfony\Component\HttpFoundation\Tests\JsonSerializableObject::jsonSerialize()
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php#114688
*/
public function testSetContentJsonSerializeError()
{
if (!interface_exists('JsonSerializable')) {
$this->markTestSkipped('Interface JsonSerializable is available in PHP 5.4+');
}

$serializable = new JsonSerializableObject();

JsonResponse::create($serializable);
}
}

if (interface_exists('JsonSerializable')) {
class JsonSerializableObject implements \JsonSerializable
{
public function jsonSerialize()
{
trigger_error('This error is expected', E_USER_WARNING);

return array();
}
}
}