Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.
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
[zfcampus/zf-apigility#118] Ensure non-HTTP exception codes are cast …
…to 500

Per zfcampus/zf-apigility#118, this PR modifies `ApiProblem` to cast
non-HTTP exception codes to 500 to ensure that valid HTTP status codes are
returned for caught exceptions.
  • Loading branch information
weierophinney committed Jul 8, 2015
commit 17cbc8f5f4bf8623789e8bf6aad38b24e527e06d
8 changes: 8 additions & 0 deletions src/ApiProblem.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ public function __construct($status, $detail, $type = null, $title = null, array
}
}

// Ensure a valid HTTP status
if (! is_numeric($status)
|| ($status < 100)
|| ($status > 599)
) {
$status = 500;
}

$this->status = $status;
$this->detail = $detail;
$this->title = $title;
Expand Down
12 changes: 2 additions & 10 deletions src/Listener/ApiProblemListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,13 @@ public function onDispatchError(MvcEvent $e)

// Marshall an ApiProblem and view model based on the exception
$exception = $e->getParam('exception');
if ($exception instanceof ProblemExceptionInterface) {
$problem = new ApiProblem($exception->getCode(), $exception);
} elseif ($exception instanceof \Exception) {
$status = $exception->getCode();
if (0 === $status) {
$status = 500;
}
$problem = new ApiProblem($status, $exception);
} else {
if (! $exception instanceof \Exception) {
// If it's not an exception, do not know what to do.
return;
}

$e->stopPropagation();
$response = new ApiProblemResponse($problem);
$response = new ApiProblemResponse(new ApiProblem($exception->getCode(), $exception));
$e->setResponse($response);
return $response;
}
Expand Down
2 changes: 1 addition & 1 deletion test/ApiProblemResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public function testComposeApiProblemIsAccessible()
public function testOverridesReasonPhraseIfStatusCodeIsUnknown()
{
$response = new ApiProblemResponse(new ApiProblem(7, 'Random error'));
$this->assertContains('Unknown', $response->getReasonPhrase());
$this->assertContains('Internal Server Error', $response->getReasonPhrase());
}
}
22 changes: 22 additions & 0 deletions test/ApiProblemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,26 @@ public function testUsesAdditionalDetailsFromExceptionWhenProvided()
$this->assertArrayHasKey('foo', $payload);
$this->assertEquals('bar', $payload['foo']);
}

public function invalidStatusCodes()
{
return array(
'-1' => array(-1),
'0' => array(0),
'7' => array(7), // reported
'14' => array(14), // observed
'600' => array(600),
);
}

/**
* @dataProvider invalidStatusCodes
* @group zf-apigility-118
*/
public function testInvalidHttpStatusCodesAreCastTo500($code)
{
$e = new \Exception('Testing', $code);
$problem = new ApiProblem($code, $e);
$this->assertEquals(500, $problem->status);
}
}