Skip to content

Commit

Permalink
Revert custom exception, fixed phpdoc comments, Log errors on failed …
Browse files Browse the repository at this point in the history
…response. (TODO: response validation in a separate middleware)
  • Loading branch information
briedis committed Mar 18, 2017
1 parent 1adfbd5 commit cb623ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 47 deletions.
14 changes: 6 additions & 8 deletions src/Briedis/ApiBuilder/ApiMethodValidationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class ApiMethodValidationMiddleware
* @param Closure $next
* @return mixed
* @throws InvalidStructureException
* @throws InvalidResponseStructureException
*/
public function handle($request, Closure $next)
{
Expand Down Expand Up @@ -52,13 +51,12 @@ public function handle($request, Closure $next)
try {
(new StructureValidator($apiEndpoint->getResponse()))->validate($response->getOriginalContent());
} catch (InvalidStructureException $e) {
// Throw a invalid response exception that basically wraps the invalid structure exception
// This will help to handle response exceptions specifically
$responseException = new InvalidResponseStructureException;
$responseException->setBadFields($e->getBadFields());
$responseException->setMissingFields($e->getMissingFields());
$responseException->setUnexpectedFields($e->getUnexpectedFields());
throw $e;
// This will do for now, but later we should implement a smarter way to throw notify about errors
\Log::error($e->getFormattedMessage(), [
'bad' => $e->getBadFields(),
'missing' => $e->getMissingFields(),
'unexpected' => $e->getUnexpectedFields(),
]);
}

return $response;
Expand Down

This file was deleted.

36 changes: 10 additions & 26 deletions src/Briedis/ApiBuilder/Exceptions/InvalidStructureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,9 @@ class InvalidStructureException extends Exception
*/
private $missingFields = [];

/**
* @param BaseItem[] $badFields
*/
public function setBadFields(array $badFields)
{
$this->badFields = $badFields;
}

/**
* @param \string[] $unexpectedFields
*/
public function setUnexpectedFields(array $unexpectedFields)
{
$this->unexpectedFields = $unexpectedFields;
}

/**
* @param BaseItem[] $missingFields
*/
public function setMissingFields(array $missingFields)
{
$this->missingFields = $missingFields;
}

/**
* Attach a bad field
*
* @param string $name
* @param BaseItem $expectedItem
*/
Expand All @@ -65,6 +42,7 @@ public function addBadField($name, BaseItem $expectedItem)

/**
* Attach a field that was given, but was unexpected
*
* @param $name
*/
public function addUnexpectedField($name)
Expand All @@ -74,6 +52,7 @@ public function addUnexpectedField($name)

/**
* Get array with fields with errors [field name => expected item]
*
* @return BaseItem[]
*/
public function getBadFields()
Expand All @@ -90,15 +69,19 @@ public function hasErrors()
}

/**
* @return array
* List of unexpected field names
*
* @return string[]
*/
public function getUnexpectedFields()
{
return $this->unexpectedFields;
}

/**
* @return array
* List of items indexed by field names
*
* @return BaseItem[]
*/
public function getMissingFields()
{
Expand All @@ -107,6 +90,7 @@ public function getMissingFields()

/**
* Add a missing field
*
* @param string $name
* @param BaseItem $expectedItem
*/
Expand Down

0 comments on commit cb623ed

Please sign in to comment.