Skip to content

Commit 283cdc5

Browse files
authored
Merge pull request #169 from WoltLab/json-response-error
Correctly handle explicitly providing `JSON_THROW_ON_ERROR` in `JsonResponse`
2 parents f6defb0 + cdf1a30 commit 283cdc5

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/Response/JsonResponse.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44

55
namespace Laminas\Diactoros\Response;
66

7+
use JsonException;
78
use Laminas\Diactoros\Exception;
89
use Laminas\Diactoros\Response;
910
use Laminas\Diactoros\Stream;
1011

1112
use function is_object;
1213
use function is_resource;
1314
use function json_encode;
14-
use function json_last_error;
15-
use function json_last_error_msg;
1615
use function sprintf;
1716

18-
use const JSON_ERROR_NONE;
1917
use const JSON_HEX_AMP;
2018
use const JSON_HEX_APOS;
2119
use const JSON_HEX_QUOT;
2220
use const JSON_HEX_TAG;
21+
use const JSON_THROW_ON_ERROR;
2322
use const JSON_UNESCAPED_SLASHES;
2423

2524
/**
@@ -131,17 +130,15 @@ private function jsonEncode(mixed $data, int $encodingOptions): string
131130
// Clear json_last_error()
132131
json_encode(null);
133132

134-
$json = json_encode($data, $encodingOptions);
135-
136-
if (JSON_ERROR_NONE !== json_last_error()) {
133+
try {
134+
return json_encode($data, $encodingOptions | JSON_THROW_ON_ERROR);
135+
} catch (JsonException $e) {
137136
throw new Exception\InvalidArgumentException(sprintf(
138137
'Unable to encode data to JSON in %s: %s',
139138
self::class,
140-
json_last_error_msg()
141-
));
139+
$e->getMessage()
140+
), 0, $e);
142141
}
143-
144-
return $json;
145142
}
146143

147144
private function setPayload(mixed $data): void

test/Response/JsonResponseTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use const JSON_HEX_QUOT;
2020
use const JSON_HEX_TAG;
2121
use const JSON_PRETTY_PRINT;
22+
use const JSON_THROW_ON_ERROR;
2223
use const JSON_UNESCAPED_SLASHES;
2324

2425
class JsonResponseTest extends TestCase
@@ -103,6 +104,22 @@ public function testJsonErrorHandlingOfBadEmbeddedData(): void
103104
new JsonResponse($data);
104105
}
105106

107+
public function testJsonErrorHandlingOfMalformedUtf8(): void
108+
{
109+
$this->expectException(InvalidArgumentException::class);
110+
$this->expectExceptionMessage('Unable to encode');
111+
112+
new JsonResponse("\xff");
113+
}
114+
115+
public function testJsonErrorHandlingOfMalformedUtf8IfExplicitlySettingThrowFlag(): void
116+
{
117+
$this->expectException(InvalidArgumentException::class);
118+
$this->expectExceptionMessage('Unable to encode');
119+
120+
new JsonResponse("\xff", encodingOptions: JsonResponse::DEFAULT_JSON_FLAGS | JSON_THROW_ON_ERROR);
121+
}
122+
106123
/** @return non-empty-array<non-empty-string, array{non-empty-string, non-empty-string}> */
107124
public function valuesToJsonEncode(): array
108125
{

0 commit comments

Comments
 (0)