Skip to content

Commit 4485859

Browse files
committed
Merge branch 'hotfix/49'
Close #49
2 parents 4209393 + 2ba0cec commit 4485859

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#49](https://github.com/zfcampus/zf-api-problem/pull/49) fixes `ApiProblem`'s
22+
detail message marshaling when PHP 7 `Throwable` types are provided as the
23+
detail.
2224

2325
## 1.2.1 - 2016-07-07
2426

src/ApiProblem.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
/**
44
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
5-
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @copyright Copyright (c) 2014-2016 Zend Technologies USA Inc. (http://www.zend.com)
66
*/
77

88
namespace ZF\ApiProblem;
99

10+
use Exception;
11+
use Throwable;
12+
use ZF\ApiProblem\Exception\InvalidArgumentException;
13+
use ZF\ApiProblem\Exception\ProblemExceptionInterface;
14+
1015
/**
1116
* Object describing an API-Problem payload.
1217
*/
@@ -34,7 +39,7 @@ class ApiProblem
3439
/**
3540
* Description of the specific problem.
3641
*
37-
* @var string|\Exception
42+
* @var string|Exception|Throwable
3843
*/
3944
protected $detail = '';
4045

@@ -128,14 +133,14 @@ class ApiProblem
128133
* from $problemStatusTitles as a result.
129134
*
130135
* @param int $status
131-
* @param string $detail
136+
* @param string|Exception|Throwable $detail
132137
* @param string $type
133138
* @param string $title
134139
* @param array $additional
135140
*/
136141
public function __construct($status, $detail, $type = null, $title = null, array $additional = [])
137142
{
138-
if ($detail instanceof Exception\ProblemExceptionInterface) {
143+
if ($detail instanceof ProblemExceptionInterface) {
139144
if (null === $type) {
140145
$type = $detail->getType();
141146
}
@@ -170,10 +175,8 @@ public function __construct($status, $detail, $type = null, $title = null, array
170175
* Retrieve properties.
171176
*
172177
* @param string $name
173-
*
174178
* @return mixed
175-
*
176-
* @throws Exception\InvalidArgumentException
179+
* @throws InvalidArgumentException
177180
*/
178181
public function __get($name)
179182
{
@@ -192,7 +195,7 @@ public function __get($name)
192195
return $this->additionalDetails[$normalized];
193196
}
194197

195-
throw new Exception\InvalidArgumentException(sprintf(
198+
throw new InvalidArgumentException(sprintf(
196199
'Invalid property name "%s"',
197200
$name
198201
));
@@ -220,7 +223,6 @@ public function toArray()
220223
* stack trace and previous exception information.
221224
*
222225
* @param bool $flag
223-
*
224226
* @return ApiProblem
225227
*/
226228
public function setDetailIncludesStackTrace($flag)
@@ -240,7 +242,7 @@ public function setDetailIncludesStackTrace($flag)
240242
*/
241243
protected function getDetail()
242244
{
243-
if ($this->detail instanceof \Exception) {
245+
if ($this->detail instanceof Throwable || $this->detail instanceof Exception) {
244246
return $this->createDetailFromException();
245247
}
246248

@@ -257,7 +259,7 @@ protected function getDetail()
257259
*/
258260
protected function getStatus()
259261
{
260-
if ($this->detail instanceof \Exception) {
262+
if ($this->detail instanceof Throwable || $this->detail instanceof Exception) {
261263
$this->status = $this->createStatusFromException();
262264
}
263265

@@ -290,7 +292,7 @@ protected function getTitle()
290292
return $this->problemStatusTitles[$this->status];
291293
}
292294

293-
if ($this->detail instanceof \Exception) {
295+
if ($this->detail instanceof Throwable || $this->detail instanceof Exception) {
294296
return get_class($this->detail);
295297
}
296298

@@ -308,6 +310,7 @@ protected function getTitle()
308310
*/
309311
protected function createDetailFromException()
310312
{
313+
/** @var Exception|Throwable $e */
311314
$e = $this->detail;
312315

313316
if (!$this->detailIncludesStackTrace) {
@@ -337,17 +340,18 @@ protected function createDetailFromException()
337340
/**
338341
* Create HTTP status from an exception.
339342
*
340-
* @return string
343+
* @return int
341344
*/
342345
protected function createStatusFromException()
343346
{
347+
/** @var Exception|Throwable $e */
344348
$e = $this->detail;
345349
$status = $e->getCode();
346350

347-
if (!empty($status)) {
351+
if ($status) {
348352
return $status;
349-
} else {
350-
return 500;
351353
}
354+
355+
return 500;
352356
}
353357
}

test/ApiProblemTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ public function testStatusIsUsedVerbatim($status)
4040
$this->assertEquals($status, $payload['status']);
4141
}
4242

43+
/**
44+
* @requires PHP 7.0
45+
*/
46+
public function testErrorAsDetails()
47+
{
48+
$error = new \TypeError('error message', 705);
49+
$apiProblem = new ApiProblem(500, $error);
50+
$payload = $apiProblem->toArray();
51+
52+
$this->assertArrayHasKey('title', $payload);
53+
$this->assertEquals('TypeError', $payload['title']);
54+
$this->assertArrayHasKey('status', $payload);
55+
$this->assertEquals(705, $payload['status']);
56+
$this->assertArrayHasKey('detail', $payload);
57+
$this->assertEquals('error message', $payload['detail']);
58+
}
59+
4360
public function testExceptionCodeIsUsedForStatus()
4461
{
4562
$exception = new \Exception('exception message', 401);

0 commit comments

Comments
 (0)