Skip to content

Commit 3e88272

Browse files
committed
Now you can override the middleware and implement your own invalid response handling mechanism.
1 parent edad841 commit 3e88272

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/Briedis/ApiBuilder/ApiMethodValidationMiddleware.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Briedis\ApiBuilder;
55

66

7-
use Briedis\ApiBuilder\Exceptions\InvalidResponseStructureException;
87
use Briedis\ApiBuilder\Exceptions\InvalidStructureException;
98
use Closure;
109

@@ -52,13 +51,29 @@ public function handle($request, Closure $next)
5251
(new StructureValidator($apiEndpoint->getResponse()))->validate($response->getOriginalContent());
5352
} catch (InvalidStructureException $e) {
5453
// This will do for now, but later we should implement a smarter way to throw notify about errors
55-
\Log::error($e->getFormattedMessage(), [
56-
'bad' => $e->getBadFields(),
57-
'missing' => $e->getMissingFields(),
58-
'unexpected' => $e->getUnexpectedFields(),
59-
]);
54+
return $this->onInvalidResponseStructure($e, $response);
6055
}
6156

6257
return $response;
6358
}
59+
60+
/**
61+
* Handle what happens when response structure is not valid. By default, we log an error.
62+
* To perform other actions,
63+
*
64+
* @param InvalidStructureException $e
65+
* @param $response
66+
* @return mixed Response that will get returned
67+
*/
68+
protected function onInvalidResponseStructure(InvalidStructureException $e, $response)
69+
{
70+
/** @noinspection PhpUndefinedClassInspection */
71+
\Log::error('Response structure is not valid: ' . $e->getFormattedMessage(), [
72+
'bad' => $e->getBadFields(),
73+
'missing' => $e->getMissingFields(),
74+
'unexpected' => $e->getUnexpectedFields(),
75+
]);
76+
77+
return $response;
78+
}
6479
}

0 commit comments

Comments
 (0)