-
Notifications
You must be signed in to change notification settings - Fork 37
Fixed issue with Throwable being passed to createApiProblemFromException #103
Changes from all commits
a95c2bd
7fe0be3
0ace572
5f1e3bb
d14863f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
namespace ZF\Rest; | ||
|
||
use ArrayAccess; | ||
use Exception; | ||
use Throwable; | ||
use Traversable; | ||
use Zend\Http\Header\Allow; | ||
use Zend\Http\Response; | ||
|
@@ -368,10 +370,10 @@ public function create($data) | |
|
||
try { | ||
$value = $this->getResource()->create($data); | ||
} catch (\Throwable $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (\Exception $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (Throwable $e) { | ||
return $this->createApiProblem($e); | ||
} catch (Exception $e) { | ||
return $this->createApiProblem($e); | ||
} | ||
|
||
if ($this->isPreparedResponse($value)) { | ||
|
@@ -427,10 +429,10 @@ public function delete($id) | |
|
||
try { | ||
$result = $this->getResource()->delete($id); | ||
} catch (\Throwable $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (\Exception $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (Throwable $e) { | ||
return $this->createApiProblem($e); | ||
} catch (Exception $e) { | ||
return $this->createApiProblem($e); | ||
} | ||
|
||
$result = $result ?: new ApiProblem(422, 'Unable to delete entity.'); | ||
|
@@ -460,10 +462,10 @@ public function deleteList($data) | |
|
||
try { | ||
$result = $this->getResource()->deleteList($data); | ||
} catch (\Throwable $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (\Exception $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (Throwable $e) { | ||
return $this->createApiProblem($e); | ||
} catch (Exception $e) { | ||
return $this->createApiProblem($e); | ||
} | ||
|
||
$result = $result ?: new ApiProblem(422, 'Unable to delete collection.'); | ||
|
@@ -494,10 +496,10 @@ public function get($id) | |
|
||
try { | ||
$entity = $this->getResource()->fetch($id); | ||
} catch (\Throwable $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (\Exception $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (Throwable $e) { | ||
return $this->createApiProblem($e); | ||
} catch (Exception $e) { | ||
return $this->createApiProblem($e); | ||
} | ||
|
||
$entity = $entity ?: new ApiProblem(404, 'Entity not found.'); | ||
|
@@ -520,7 +522,7 @@ public function get($id) | |
/** | ||
* Return collection of entities | ||
* | ||
* @return Response|HalCollection | ||
* @return Response|HalCollection|ApiProblem | ||
*/ | ||
public function getList() | ||
{ | ||
|
@@ -529,10 +531,10 @@ public function getList() | |
|
||
try { | ||
$collection = $this->getResource()->fetchAll(); | ||
} catch (\Throwable $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (\Exception $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (Throwable $e) { | ||
return $this->createApiProblem($e); | ||
} catch (Exception $e) { | ||
return $this->createApiProblem($e); | ||
} | ||
|
||
if ($this->isPreparedResponse($collection)) { | ||
|
@@ -642,10 +644,10 @@ public function patch($id, $data) | |
|
||
try { | ||
$entity = $this->getResource()->patch($id, $data); | ||
} catch (\Throwable $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (\Exception $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (Throwable $e) { | ||
return $this->createApiProblem($e); | ||
} catch (Exception $e) { | ||
return $this->createApiProblem($e); | ||
} | ||
|
||
if ($this->isPreparedResponse($entity)) { | ||
|
@@ -679,10 +681,10 @@ public function update($id, $data) | |
|
||
try { | ||
$entity = $this->getResource()->update($id, $data); | ||
} catch (\Throwable $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (\Exception $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (Throwable $e) { | ||
return $this->createApiProblem($e); | ||
} catch (Exception $e) { | ||
return $this->createApiProblem($e); | ||
} | ||
|
||
if ($this->isPreparedResponse($entity)) { | ||
|
@@ -706,7 +708,7 @@ public function update($id, $data) | |
* a collection, i.e. create and/or update multiple entities in a collection. | ||
* | ||
* @param array $data | ||
* @return array | ||
* @return array|ApiProblem | ||
*/ | ||
public function patchList($data) | ||
{ | ||
|
@@ -715,10 +717,10 @@ public function patchList($data) | |
|
||
try { | ||
$collection = $this->getResource()->patchList($data); | ||
} catch (\Throwable $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (\Exception $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (Throwable $e) { | ||
return $this->createApiProblem($e); | ||
} catch (Exception $e) { | ||
return $this->createApiProblem($e); | ||
} | ||
|
||
if ($this->isPreparedResponse($collection)) { | ||
|
@@ -739,7 +741,7 @@ public function patchList($data) | |
* Update an existing collection of entities | ||
* | ||
* @param array $data | ||
* @return array | ||
* @return array|ApiProblem | ||
*/ | ||
public function replaceList($data) | ||
{ | ||
|
@@ -750,10 +752,10 @@ public function replaceList($data) | |
$collection = $this->getResource()->replaceList($data); | ||
} catch (Exception\InvalidArgumentException $e) { | ||
return new ApiProblem(400, $e->getMessage()); | ||
} catch (\Throwable $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (\Exception $e) { | ||
return $this->createApiProblemFromException($e); | ||
} catch (Throwable $e) { | ||
return $this->createApiProblem($e); | ||
} catch (Exception $e) { | ||
return $this->createApiProblem($e); | ||
} | ||
|
||
if ($this->isPreparedResponse($collection)) { | ||
|
@@ -809,33 +811,6 @@ protected function createAllowHeaderWithAllowedMethods(array $methods) | |
return $allow; | ||
} | ||
|
||
/** | ||
* @param \Exception $e | ||
* @return ApiProblem | ||
*/ | ||
protected function createApiProblemFromException(\Exception $e) | ||
{ | ||
return new ApiProblem($this->getHttpStatusCodeFromException($e), $e); | ||
} | ||
|
||
/** | ||
* Ensure we have a valid HTTP status code for an ApiProblem | ||
* | ||
* @param \Exception $e | ||
* @return int | ||
*/ | ||
protected function getHttpStatusCodeFromException(\Exception $e) | ||
{ | ||
$code = $e->getCode(); | ||
if (!is_int($code) | ||
|| $code < 100 | ||
|| $code >= 600 | ||
) { | ||
return 500; | ||
} | ||
return $code; | ||
} | ||
|
||
/** | ||
* Injects the resource with the identity composed in the event, if present | ||
*/ | ||
|
@@ -998,4 +973,34 @@ protected function createHalEntity($entity) | |
$this->getRouteIdentifierName() | ||
); | ||
} | ||
|
||
/** | ||
* @param Exception|Throwable $error | ||
* @return ApiProblem | ||
*/ | ||
private function createApiProblem($error) | ||
{ | ||
return new ApiProblem( | ||
$this->validateHttpStatusCode($error->getCode()), | ||
$error->getMessage() | ||
); | ||
} | ||
|
||
/** | ||
* Ensure we have a valid HTTP status code for an ApiProblem | ||
* | ||
* @param int|string $code | ||
* @return int | ||
*/ | ||
private function validateHttpStatusCode($code) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just like the |
||
{ | ||
if (!is_int($code) | ||
|| $code < 100 | ||
|| $code >= 600 | ||
) { | ||
return 500; | ||
} | ||
|
||
return $code; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In reviewing, I realized something: the original was protected, so that it could be called by extending classes. Let's make it protected again, and reinstate the original name (
createApiProblemFromException()
). Update the docblock to indicate that the$error
argument can be either a PHP 7 Throwable or any Exception type.The argument can drop the typehint safely in a maintenance release, as that's a widening of types, vs. narrowing.