Skip to content

Commit

Permalink
Now you can override the middleware and implement your own invalid re…
Browse files Browse the repository at this point in the history
…sponse handling mechanism.
  • Loading branch information
briedis committed Apr 2, 2017
1 parent edad841 commit 3e88272
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/Briedis/ApiBuilder/ApiMethodValidationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Briedis\ApiBuilder;


use Briedis\ApiBuilder\Exceptions\InvalidResponseStructureException;
use Briedis\ApiBuilder\Exceptions\InvalidStructureException;
use Closure;

Expand Down Expand Up @@ -52,13 +51,29 @@ public function handle($request, Closure $next)
(new StructureValidator($apiEndpoint->getResponse()))->validate($response->getOriginalContent());
} catch (InvalidStructureException $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 $this->onInvalidResponseStructure($e, $response);
}

return $response;
}

/**
* Handle what happens when response structure is not valid. By default, we log an error.
* To perform other actions,
*
* @param InvalidStructureException $e
* @param $response
* @return mixed Response that will get returned
*/
protected function onInvalidResponseStructure(InvalidStructureException $e, $response)
{
/** @noinspection PhpUndefinedClassInspection */
\Log::error('Response structure is not valid: ' . $e->getFormattedMessage(), [
'bad' => $e->getBadFields(),
'missing' => $e->getMissingFields(),
'unexpected' => $e->getUnexpectedFields(),
]);

return $response;
}
}

0 comments on commit 3e88272

Please sign in to comment.