Skip to content

Commit 8e2263b

Browse files
committed
Small fixed, test added
1 parent 1eb10cc commit 8e2263b

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

src/ApiRequestException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace PhpInsights;
3+
4+
use PhpInsights\Result\InsightsException;
5+
6+
class ApiRequestException extends InsightsException
7+
{
8+
9+
}

src/InsightsCaller.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
namespace PhpInsights;
33

4-
54
use GuzzleHttp\Client;
5+
use GuzzleHttp\Exception\TransferException;
66

77
class InsightsCaller
88
{
@@ -57,10 +57,20 @@ public function __invoke($url, $strategy = self::STRATEGY_MOBILE)
5757
* @param string $strategy
5858
*
5959
* @return InsightsResponse
60+
*
61+
* @throws ApiRequestException
6062
*/
61-
public function getResponse($url, $strategy = 'mobile') {
63+
public function getResponse($url, $strategy = 'mobile')
64+
{
6265
$apiEndpoint = $this->createApiEndpointUrl($url, $strategy);
63-
return InsightsResponse::fromResponse($this->client->request('GET', $apiEndpoint));
66+
67+
try {
68+
$response = $this->client->request('GET', $apiEndpoint);
69+
} catch (TransferException $e) {
70+
throw new ApiRequestException($e->getMessage());
71+
}
72+
73+
return InsightsResponse::fromResponse($response);
6474

6575
}
6676

@@ -81,7 +91,6 @@ public function setCaptureScreenshot($captureScreenshot)
8191
}
8292

8393

84-
8594
/**
8695
* @param string $url
8796
* @param string $strategy
@@ -95,5 +104,4 @@ protected function createApiEndpointUrl($url, $strategy = 'mobile')
95104
}
96105

97106

98-
99107
}

src/InvalidJsonException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace PhpInsights;
33

4-
class InvalidJsonException extends \Exception
4+
use PhpInsights\Result\InsightsException;
5+
6+
class InvalidJsonException extends InsightsException
57
{
68

79
}

src/Result/InsightsException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PhpInsights\Result;
4+
5+
class InsightsException extends \Exception
6+
{
7+
8+
}

tests/InsightsCallerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ public function testCanConstructed()
1010
return $this->assertInstanceOf('\PhpInsights\InsightsCaller', $caller);
1111

1212
}
13+
14+
public function testInvalidApiKey()
15+
{
16+
$this->expectException(\PhpInsights\ApiRequestException::class);
17+
$caller = new \PhpInsights\InsightsCaller('foo');
18+
$caller->getResponse('foo');
19+
}
20+
1321
}

0 commit comments

Comments
 (0)