Skip to content

Commit 17af341

Browse files
committed
Add ExceptionHandler::acceptJson method
1 parent 305bc71 commit 17af341

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/ExceptionHandler.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function exceptionHandler(Throwable $exception) : void
169169
if (!\headers_sent()) {
170170
$this->sendHeaders();
171171
}
172-
if ($this->isJson()) {
172+
if ($this->isJson() || $this->acceptJson()) {
173173
$this->sendJson($exception);
174174
return;
175175
}
@@ -193,6 +193,12 @@ protected function isJson() : bool
193193
&& \str_starts_with($_SERVER['HTTP_CONTENT_TYPE'], 'application/json');
194194
}
195195

196+
protected function acceptJson() : bool
197+
{
198+
return isset($_SERVER['HTTP_ACCEPT'])
199+
&& \str_contains($_SERVER['HTTP_ACCEPT'], 'application/json');
200+
}
201+
196202
protected function sendJson(Throwable $exception) : void
197203
{
198204
$data = $this->getEnvironment() === static::DEVELOPMENT
@@ -218,7 +224,7 @@ protected function sendJson(Throwable $exception) : void
218224
protected function sendHeaders() : void
219225
{
220226
$contentType = 'text/html';
221-
if ($this->isJson()) {
227+
if ($this->isJson() || $this->acceptJson()) {
222228
$contentType = 'application/json';
223229
}
224230
\header('Content-Type: ' . $contentType . '; charset=UTF-8');

tests/ExceptionHandlerTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ public function testCliException() : void
7979
}
8080

8181
/**
82+
* @dataProvider jsonHeadersProvider
83+
*
8284
* @runInSeparateProcess
8385
*/
84-
public function testJsonExceptionOnProduction() : void
86+
public function testJsonExceptionOnProduction(string $header, string $value) : void
8587
{
86-
$_SERVER['HTTP_CONTENT_TYPE'] = 'application/json ';
88+
$_SERVER[$header] = $value;
8789
$exceptions = new ExceptionHandlerMock();
8890
$exceptions->cli = false;
8991
\ob_start();
@@ -106,11 +108,13 @@ public function testJsonExceptionOnProduction() : void
106108
}
107109

108110
/**
111+
* @dataProvider jsonHeadersProvider
112+
*
109113
* @runInSeparateProcess
110114
*/
111-
public function testJsonExceptionOnDevelopment() : void
115+
public function testJsonExceptionOnDevelopment(string $header, string $value) : void
112116
{
113-
$_SERVER['HTTP_CONTENT_TYPE'] = 'application/json ';
117+
$_SERVER[$header] = $value;
114118
$exceptions = new ExceptionHandlerMock(ExceptionHandler::DEVELOPMENT);
115119
$exceptions->cli = false;
116120
\ob_start();
@@ -237,4 +241,15 @@ public static function errorProvider() : array
237241
[\E_USER_NOTICE, 'User Notice'],
238242
];
239243
}
244+
245+
/**
246+
* @return array<array<string>>
247+
*/
248+
public static function jsonHeadersProvider() : array
249+
{
250+
return [
251+
['HTTP_ACCEPT', 'text/html; application/json'],
252+
['HTTP_CONTENT_TYPE', 'application/json; charset=UTF-8'],
253+
];
254+
}
240255
}

0 commit comments

Comments
 (0)