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

[8.x] Use standard InvalidArgumentException when json_encode() failed #36868

Merged
merged 3 commits into from
Apr 4, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Illuminate/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use JsonSerializable;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use UnexpectedValueException;

class Response extends SymfonyResponse
{
Expand Down Expand Up @@ -42,6 +42,8 @@ public function __construct($content = '', $status = 200, array $headers = [])
*
* @param mixed $content
* @return $this
*
* @throws \InvalidArgumentException
*/
public function setContent($content)
{
Expand All @@ -56,7 +58,7 @@ public function setContent($content)
$content = $this->morphToJson($content);

if ($content === false) {
throw new UnexpectedValueException(sprintf('Unable to convert the provided response data to JSON: %s', json_last_error_msg()));
throw new InvalidArgumentException(json_last_error_msg());
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Integration/Http/JsonResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Illuminate\Tests\Integration\Http;

use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\TestCase;

/**
* @group integration
*/
class JsonResponseTest extends TestCase
{
public function testResponseWithInvalidJsonThrowsException()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');

Route::get('/response', function () {
return new JsonResponse(new class implements \JsonSerializable {
public function jsonSerialize()
{
return "\xB1\x31";
}
});
});

$this->withoutExceptionHandling();

$this->get('/response');
}
}
32 changes: 32 additions & 0 deletions tests/Integration/Http/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Illuminate\Tests\Integration\Http;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\TestCase;

/**
* @group integration
*/
class ResponseTest extends TestCase
{
public function testResponseWithInvalidJsonThrowsException()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');

Route::get('/response', function () {
return (new Response())->setContent(new class implements \JsonSerializable {
public function jsonSerialize()
{
return "\xB1\x31";
}
});
});

$this->withoutExceptionHandling();

$this->get('/response');
}
}