8000 [HttpFoundation] Silent only JSON errors · symfony/symfony@ddf95c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit ddf95c7

Browse files
GromNaNfabpot
authored andcommitted
[HttpFoundation] Silent only JSON errors
1 parent 4fa670b commit ddf95c7

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,20 @@ public function setCallback($callback = null)
9595
*/
9696
public function setData($data = array())
9797
{
98-
$this->data = @json_encode($data, $this->encodingOptions);
98+
$errorHandler = null;
99+
$errorHandler = set_error_handler(function () use (&$errorHandler) {
100+
if (JSON_ERROR_NONE !== json_last_error()) {
101+
return;
102+
}
103+
104+
if ($errorHandler) {
105+
call_user_func_array($errorHandler, func_get_args());
106+
}
107+
});
108+
109+
$this->data = json_encode($data, $this->encodingOptions);
110+
111+
restore_error_handler();
99112

100113
if (JSON_ERROR_NONE !== json_last_error()) {
101114
throw new \InvalidArgumentException($this->transformJsonError());

src/Symfony/Component/HttpFoundation/Tests/JsonResponseTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,31 @@ public function testSetContent()
201201
{
202202
JsonResponse::create("\xB1\x31");
203203
}
204+
205+
/**
206+
* @expectedException PHPUnit_Framework_Error_Warning
207+
* @expectedExceptionMessage This error is expected
208+
*/
209+
public function testSetContentJsonSerializeError()
210+
{
211+
if (!interface_exists('JsonSerializable')) {
212+
$this->markTestSkipped('Interface JsonSerializable is available in PHP 5.4+');
213+
}
214+
215+
$serializable = new JsonSerializableObject();
216+
217+
JsonResponse::create($serializable);
218+
}
219+
}
220+
221+
if (interface_exists('JsonSerializable')) {
222+
class JsonSerializableObject implements JsonSerializable
223+
{
224+
public function jsonSerialize()
225+
{
226+
trigger_error('This error is expected', E_USER_WARNING);
227+
228+
return array();
229+
}
230+
}
204231
}

0 commit comments

Comments
 (0)
0