From c32b6c92bd2267e27600a6c4115f78c1c1fb9bfb Mon Sep 17 00:00:00 2001 From: Edward Ver Date: Wed, 3 Oct 2018 11:27:32 +0200 Subject: [PATCH] Catch empty body error (#92) * when statuscode is not 200 throw error with ReasonPhrase * test for http error with empty body * remove empty lines * syntax fix --- src/Instagram.php | 4 ++++ tests/InstagramTest.php | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Instagram.php b/src/Instagram.php index e811f94..29f0724 100644 --- a/src/Instagram.php +++ b/src/Instagram.php @@ -119,6 +119,10 @@ protected function get(string $path): object throw new InstagramException($body->meta->error_message); } + if ($response->getStatusCode() !== 200) { + throw new InstagramException($response->getReasonPhrase()); + } + return $body; } } diff --git a/tests/InstagramTest.php b/tests/InstagramTest.php index 3516276..8d7dbc3 100644 --- a/tests/InstagramTest.php +++ b/tests/InstagramTest.php @@ -66,4 +66,18 @@ public function testError() (new Instagram('imspeechlessihavenospeech'))->media(); } + + public function testHttpError() + { + $this->expectException(InstagramException::class); + $this->expectExceptionMessage('No server is available for the request'); + + $response = new Response(503, [], null, null, 'No server is available for the request'); + + $client = new Client(); + $client->addResponse($response); + + $instagram = new Instagram('jerryseinfeld', $client); + $user = $instagram->self(); + } }