|
4 | 4 | namespace Briedis\ApiBuilder; |
5 | 5 |
|
6 | 6 |
|
7 | | -use Briedis\ApiBuilder\Exceptions\InvalidResponseStructureException; |
8 | 7 | use Briedis\ApiBuilder\Exceptions\InvalidStructureException; |
9 | 8 | use Closure; |
10 | 9 |
|
@@ -52,13 +51,29 @@ public function handle($request, Closure $next) |
52 | 51 | (new StructureValidator($apiEndpoint->getResponse()))->validate($response->getOriginalContent()); |
53 | 52 | } catch (InvalidStructureException $e) { |
54 | 53 | // 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); |
60 | 55 | } |
61 | 56 |
|
62 | 57 | return $response; |
63 | 58 | } |
| 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 | + } |
64 | 79 | } |
0 commit comments