From 3e882729c67fe7e3ca9beb14c698e3c6488ec4b2 Mon Sep 17 00:00:00 2001 From: "martins.briedis" Date: Sun, 2 Apr 2017 13:58:24 +0300 Subject: [PATCH] Now you can override the middleware and implement your own invalid response handling mechanism. --- .../ApiMethodValidationMiddleware.php | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Briedis/ApiBuilder/ApiMethodValidationMiddleware.php b/src/Briedis/ApiBuilder/ApiMethodValidationMiddleware.php index c907f38..315f492 100644 --- a/src/Briedis/ApiBuilder/ApiMethodValidationMiddleware.php +++ b/src/Briedis/ApiBuilder/ApiMethodValidationMiddleware.php @@ -4,7 +4,6 @@ namespace Briedis\ApiBuilder; -use Briedis\ApiBuilder\Exceptions\InvalidResponseStructureException; use Briedis\ApiBuilder\Exceptions\InvalidStructureException; use Closure; @@ -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; + } }