Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 323710a

Browse files
committed
bug #9908 [HttpFoundation] Throw proper exception when invalid data is passed to JsonResponse class (stloyd)
This PR was merged into the 2.3 branch. Discussion ---------- [HttpFoundation] Throw proper exception when invalid data is passed to JsonResponse class | Q | A | ------------- | --- | Bug fix? | yes | BC breaks? | no* | Tests pass? | yes | Fixed tickets | #9903 | License | MIT \* as described in mentioned issue, before this PR there was thrown exception `UnexpectedValueException`, which was not correct, but I guess some people trying to hide the bug could use `try {} catch` on it. Commits ------- 38287e7 [HttpFoundation] Throw proper exception when invalid data is passed to JsonResponse class
2 parents 3704e08 + 38287e7 commit 323710a

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/Symfony/Component/HttpFoundation/JsonResponse.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,18 @@ public function setCallback($callback = null)
8585
* @param mixed $data
8686
*
8787
* @return JsonResponse
88+
*
89+
* @throws \InvalidArgumentException
8890
*/
8991
public function setData($data = array())
9092
{
9193
// Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML.
9294
$this->data = json_encode($data, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
9395

96+
if (JSON_ERROR_NONE !== json_last_error()) {
97+
throw new \InvalidArgumentException($this->transformJsonError());
98+
}
99+
94100
return $this->update();
95101
}
96102

@@ -116,4 +122,31 @@ protected function update()
116122

117123
return $this->setContent($this->data);
118124
}
125+
126+
private function transformJsonError()
127+
{
128+
if (function_exists('json_last_error_msg')) {
129+
return json_last_error_msg();
130+
}
131+
132+
switch (json_last_error()) {
133+
case JSON_ERROR_DEPTH:
134+
return 'Maximum stack depth exceeded.';
135+
136+
case JSON_ERROR_STATE_MISMATCH:
137+
return 'Underflow or the modes mismatch.';
138+
139+
case JSON_ERROR_CTRL_CHAR:
140+
return 'Unexpected control character found.';
141+
142+
case JSON_ERROR_SYNTAX:
143+
return 'Syntax error, malformed JSON.';
144+
145+
case JSON_ERROR_UTF8:
146+
return 'Malformed UTF-8 characters, possibly incorrectly encoded.';
147+
148+
default:
149+
return 'Unknown error.';
150+
}
151+
}
119152
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,27 @@ public function testSetCallback()
159159
$this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
160160
}
161161

162+
public function testJsonEncodeFlags()
163+
{
164+
$response = new JsonResponse('<>\'&"');
165+
166+
$this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
167+
}
168+
169+
/**
170+
* @expectedException \InvalidArgumentException
171+
*/
162172
public function testSetCallbackInvalidIdentifier()
163173
{
164174
$response = new JsonResponse('foo');
165-
166-
$this->setExpectedException('InvalidArgumentException');
167175
$response->setCallback('+invalid');
168176
}
169177

170-
public function testJsonEncodeFlags()
178+
/**
179+
* @expectedException \InvalidArgumentException
180+
*/
181+
public function testSetContent()
171182
{
172-
$response = new JsonResponse('<>\'&"');
173-
174-
$this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
183+
JsonResponse::create("\xB1\x31");
175184
}
176185
}

0 commit comments

Comments
 (0)