Skip to content
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

Use JSON_THROW_ON_ERROR instead of custom error handling #32628

Merged
merged 1 commit into from May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 1 addition & 7 deletions lib/public/AppFramework/Http/JSONResponse.php
Expand Up @@ -64,13 +64,7 @@ public function __construct($data = [], $statusCode = Http::STATUS_OK) {
* @throws \Exception If data could not get encoded
*/
public function render() {
$response = json_encode($this->data, JSON_HEX_TAG);
if ($response === false) {
throw new \Exception(sprintf('Could not json_encode due to invalid ' .
'non UTF-8 characters in the array: %s', var_export($this->data, true)));
}

return $response;
return json_encode($this->data, JSON_HEX_TAG | JSON_THROW_ON_ERROR);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/AppFramework/Http/JSONResponseTest.php
Expand Up @@ -88,10 +88,10 @@ public function testRender(array $input, $expected) {
$this->assertEquals($expected, $this->json->render());
}


public function testRenderWithNonUtf8Encoding() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Could not json_encode due to invalid non UTF-8 characters in the array: array (');
$this->expectException(\JsonException::class);
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');

$params = ['test' => hex2bin('e9')];
$this->json->setData($params);
Expand Down