Skip to content

Commit 9986c42

Browse files
author
Nara Kasbergen
committed
fixes #1 - add custom ApiException class
1 parent 43134f6 commit 9986c42

File tree

7 files changed

+158
-19
lines changed

7 files changed

+158
-19
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
NPR\One\Exceptions\ApiException
2+
===============
3+
4+
An extension of CookieProvider that encrypts cookies before setting them and decrypts them when retrieving them
5+
6+
7+
8+
9+
* Class name: ApiException
10+
* Namespace: NPR\One\Exceptions
11+
* Parent class: Exception
12+
13+
14+
15+
16+
17+
18+
19+
Methods
20+
-------
21+
22+
23+
### __construct
24+
25+
mixed NPR\One\Exceptions\ApiException::__construct(string $message, \GuzzleHttp\Psr7\Response $response)
26+
27+
Constructs the exception using the response from the API call.
28+
29+
30+
31+
* Visibility: **public**
32+
33+
34+
#### Arguments
35+
* $message **string**
36+
* $response **GuzzleHttp\Psr7\Response**
37+
38+
39+
40+
### getStatusCode
41+
42+
integer NPR\One\Exceptions\ApiException::getStatusCode()
43+
44+
Returns the HTTP status code from the failed API call; should generally always be 400 or greater.
45+
46+
47+
48+
* Visibility: **public**
49+
50+
51+
52+
53+
### getBody
54+
55+
\GuzzleHttp\Psr7\Stream NPR\One\Exceptions\ApiException::getBody()
56+
57+
Returns the body of the response from the failed API call. May be empty.
58+
59+
60+
61+
* Visibility: **public**
62+
63+
64+

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ API Index
33

44
* NPR
55
* NPR\One
6+
* NPR\One\Exceptions
7+
* [ApiException](NPR-One-Exceptions-ApiException.md)
68
* NPR\One\Models
79
* [JsonModel](NPR-One-Models-JsonModel.md)
810
* [DeviceCodeModel](NPR-One-Models-DeviceCodeModel.md)

examples/Router.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use NPR\One\Controllers\DeviceCodeController;
66
use NPR\One\Controllers\LogoutController;
77
use NPR\One\Controllers\RefreshTokenController;
8+
use NPR\One\Exceptions\ApiException;
89
use Your\Package\Here\ConfigProvider;
910
use Your\Package\Here\StorageProvider;
1011

@@ -114,6 +115,11 @@
114115
'localactivation'
115116
]);
116117
}
118+
catch (ApiException $e)
119+
{
120+
$data = $e->getMessage();
121+
$statusCode = $e->getStatusCode();
122+
}
117123
catch (\Exception $e)
118124
{
119125
$data = $e->getMessage();
@@ -139,6 +145,11 @@
139145
{
140146
$data = $controller->pollDeviceCodeGrant();
141147
}
148+
catch (ApiException $e)
149+
{
150+
$data = !empty($e->getBody()) ? $e->getBody() : $e->getMessage();
151+
$statusCode = $e->getStatusCode();
152+
}
142153
catch (\Exception $e)
143154
{
144155
$data = $e->getMessage();
@@ -164,6 +175,11 @@
164175
{
165176
$data = $controller->generateNewAccessTokenFromRefreshToken();
166177
}
178+
catch (ApiException $e)
179+
{
180+
$data = $e->getMessage();
181+
$statusCode = $e->getStatusCode();
182+
}
167183
catch (\Exception $e)
168184
{
169185
$data = $e->getMessage();
@@ -186,6 +202,11 @@
186202
$controller->deleteAccessAndRefreshTokens($token);
187203
$data = ''; // there is nothing to return in the case of success; an empty string should suffice
188204
}
205+
catch (ApiException $e)
206+
{
207+
$data = $e->getMessage();
208+
$statusCode = $e->getStatusCode();
209+
}
189210
catch (\Exception $e)
190211
{
191212
$data = $e->getMessage();

src/One/Controllers/AbstractOAuth2Controller.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GuzzleHttp\Client;
66
use GuzzleHttp\Psr7\Response;
77
use NPR\One\DI\DI;
8+
use NPR\One\Exceptions\ApiException;
89
use NPR\One\Interfaces\ConfigInterface;
910
use NPR\One\Interfaces\EncryptionInterface;
1011
use NPR\One\Interfaces\StorageInterface;
@@ -209,7 +210,7 @@ final protected function validateScopes(array $scopes)
209210
* @param string[] $additionalParams
210211
* @return AccessTokenModel
211212
* @throws \InvalidArgumentException
212-
* @throws \Exception
213+
* @throws ApiException
213214
*/
214215
final protected function createAccessToken($grantType, $additionalParams = [])
215216
{
@@ -233,7 +234,7 @@ final protected function createAccessToken($grantType, $additionalParams = [])
233234

234235
if ($response->getStatusCode() >= 400)
235236
{
236-
throw new \Exception("Error during createAccessToken for grant $grantType: {$this->getResponseMessage($response)}"); // @codeCoverageIgnore
237+
throw new ApiException("Error during createAccessToken for grant $grantType", $response); // @codeCoverageIgnore
237238
}
238239

239240
$body = $response->getBody();
@@ -258,17 +259,4 @@ final protected function storeRefreshToken(AccessTokenModel $accessToken)
258259
$this->secureStorage->remove('refresh_token');
259260
}
260261
}
261-
262-
/**
263-
* Pretty print the response for inclusion in logs
264-
*
265-
* @internal
266-
* @param Response $response
267-
* @return string
268-
* @codeCoverageIgnore
269-
*/
270-
protected function getResponseMessage(Response $response)
271-
{
272-
return "status: {$response->getStatusCode()}, body: {$response->getBody()}";
273-
}
274262
}

src/One/Controllers/DeviceCodeController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\Client;
66
use NPR\One\DI\DI;
7+
use NPR\One\Exceptions\ApiException;
78
use NPR\One\Models\AccessTokenModel;
89
use NPR\One\Models\DeviceCodeModel;
910

@@ -74,7 +75,7 @@ public function pollDeviceCodeGrant()
7475
* @internal
7576
* @param string[] $scopes
7677
* @return DeviceCodeModel
77-
* @throws \Exception
78+
* @throws ApiException
7879
*/
7980
private function createDeviceCode(array $scopes)
8081
{
@@ -91,7 +92,7 @@ private function createDeviceCode(array $scopes)
9192

9293
if ($response->getStatusCode() >= 400)
9394
{
94-
throw new \Exception("Error during startDeviceCodeGrant: {$this->getResponseMessage($response)}"); // @codeCoverageIgnore
95+
throw new ApiException('Error during startDeviceCodeGrant', $response); // @codeCoverageIgnore
9596
}
9697

9798
$body = $response->getBody();

src/One/Controllers/LogoutController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\Client;
66
use NPR\One\DI\DI;
7+
use NPR\One\Exceptions\ApiException;
78

89

910
/**
@@ -53,7 +54,7 @@ public function deleteAccessAndRefreshTokens($accessToken = null)
5354
* @internal
5455
* @param string $token
5556
* @param bool $isRefreshToken
56-
* @throws \Exception
57+
* @throws ApiException
5758
*/
5859
private function revokeToken($token, $isRefreshToken = false)
5960
{
@@ -83,7 +84,7 @@ private function revokeToken($token, $isRefreshToken = false)
8384

8485
if ($response->getStatusCode() >= 400)
8586
{
86-
throw new \Exception("Error during revokeToken for token $token: {$this->getResponseMessage($response)}"); // @codeCoverageIgnore
87+
throw new ApiException("Error during revokeToken for token $token", $response); // @codeCoverageIgnore
8788
}
8889

8990
// A successful call has no response body, so there's nothing to return!

src/One/Exceptions/ApiException.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace NPR\One\Exceptions;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use GuzzleHttp\Psr7\Stream;
7+
8+
9+
/**
10+
* An extension of CookieProvider that encrypts cookies before setting them and decrypts them when retrieving them
11+
*
12+
* @package NPR\One\Exceptions
13+
* @codeCoverageIgnore
14+
*/
15+
class ApiException extends \Exception
16+
{
17+
/**
18+
* @var int
19+
* @internal
20+
*/
21+
private $statusCode;
22+
/**
23+
* @var Stream
24+
* @internal
25+
*/
26+
private $body;
27+
28+
29+
/**
30+
* Constructs the exception using the response from the API call.
31+
*
32+
* @param string $message
33+
* @param Response $response
34+
*/
35+
public function __construct($message, Response $response)
36+
{
37+
parent::__construct($message . " - status: {$response->getStatusCode()}, body: {$response->getBody()}");
38+
39+
$this->statusCode = $response->getStatusCode();
40+
$this->body = $response->getBody();
41+
}
42+
43+
/**
44+
* Returns the HTTP status code from the failed API call; should generally always be 400 or greater.
45+
*
46+
* @return int
47+
*/
48+
public function getStatusCode()
49+
{
50+
return $this->statusCode;
51+
}
52+
53+
/**
54+
* Returns the body of the response from the failed API call. May be empty.
55+
*
56+
* @return Stream
57+
*/
58+
public function getBody()
59+
{
60+
return $this->body;
61+
}
62+
}

0 commit comments

Comments
 (0)