Skip to content

Commit 5f7043e

Browse files
author
Michael Kühn
committed
use JSON_PARTIAL_OUTPUT_ON_ERROR for json_decode()
ApiProblem was not able to render exceptions when the option ApiProblem::detailIncludesStackTrace was true and the exception contained PHP streams in the arguments of the traces ("args" key in Exception::getTraces()). For example: GuzzleHttp works internally with streams which meant that some exceptions raised from GuzzleHttp didn't result in a ApiProblem response but in an empty 500 response because json_encode() failed.
1 parent ba209e6 commit 5f7043e

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/ApiProblemResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ApiProblemResponse extends Response
2323
*
2424
* @var int
2525
*/
26-
protected $jsonFlags = 0;
26+
protected $jsonFlags = JSON_PARTIAL_OUTPUT_ON_ERROR;
2727

2828
/**
2929
* @param ApiProblem $apiProblem
@@ -35,7 +35,7 @@ public function __construct(ApiProblem $apiProblem)
3535
$this->setReasonPhrase($apiProblem->title);
3636

3737
if (defined('JSON_UNESCAPED_SLASHES')) {
38-
$this->jsonFlags = constant('JSON_UNESCAPED_SLASHES');
38+
$this->jsonFlags |= constant('JSON_UNESCAPED_SLASHES');
3939
}
4040
}
4141

test/ApiProblemResponseTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,21 @@ public function testApiProblemResponseSetsStatusCodeAndReasonPhrase()
3232

3333
public function testApiProblemResponseBodyIsSerializedApiProblem()
3434
{
35-
$apiProblem = new ApiProblem(400, 'Random error');
35+
$additional = [
36+
'foo' => fopen('php://memory', 'r')
37+
];
38+
39+
$expected = [
40+
'foo' => null,
41+
'type' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html',
42+
'title' => 'Bad Request',
43+
'status' => 400,
44+
'detail' => 'Random error',
45+
];
46+
47+
$apiProblem = new ApiProblem(400, 'Random error', null, null, $additional);
3648
$response = new ApiProblemResponse($apiProblem);
37-
$this->assertEquals($apiProblem->toArray(), json_decode($response->getContent(), true));
49+
$this->assertEquals($expected, json_decode($response->getContent(), true));
3850
}
3951

4052
/**

0 commit comments

Comments
 (0)