Skip to content

Commit 5418c25

Browse files
Valentin ClavreulVincentChalnot
Valentin Clavreul
authored andcommitted
Added exceptions for client side errors (4xx) and server side errors (5xx)
1 parent c359b24 commit 5418c25

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed

Client/ApiClient.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use CleverAge\OAuthApiBundle\Exception\ApiDeserializationException;
1414
use CleverAge\OAuthApiBundle\Exception\ApiRequestException;
15+
use CleverAge\OAuthApiBundle\Exception\RequestFailedException;
1516
use CleverAge\OAuthApiBundle\Request\ApiRequestInterface;
1617
use Http\Client\Exception as HttpException;
1718
use GuzzleHttp\Psr7\Request;
@@ -127,24 +128,33 @@ protected function getResponseData(
127128
Request $request
128129
): ?string {
129130
$this->logger->debug(
130-
"API Request:{$request->getMethod()} {$request->getUri()}",
131+
"API Request",
131132
[
132-
'body' => $request->getBody(),
133+
'method' => $request->getMethod(),
134+
'uri' => $request->getUri(),
135+
'body' => (string)$request->getBody(),
133136
]
134137
);
135138
try {
136139
$response = $this->client->sendRequest($request);
137140
} catch (HttpException $e) {
138-
throw ApiRequestException::create((string) $request->getUri(), $e);
141+
throw ApiRequestException::create((string)$request->getUri(), $e);
139142
}
140-
$body = (string) $response->getBody();
143+
$body = (string)$response->getBody();
141144
$this->logger->debug(
142-
"API Response:{$request->getMethod()} {$request->getUri()}",
145+
"API Response",
143146
[
147+
'method' => $request->getMethod(),
148+
'uri' => $request->getUri(),
149+
'status_code' => $response->getStatusCode(),
144150
'body' => $body,
145151
]
146152
);
147153

154+
if ($response->getStatusCode() >= 400) {
155+
throw RequestFailedException::createFromResponse($response);
156+
}
157+
148158
return $body;
149159
}
150160

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of the CleverAge/OAuthApiBundle package.
4+
*
5+
* Copyright (C) 2017-2019 Clever-Age
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace CleverAge\OAuthApiBundle\Exception;
12+
13+
use Psr\Http\Message\ResponseInterface;
14+
15+
class ClientRequestFailedException extends RequestFailedException
16+
{
17+
public function __construct(ResponseInterface $response, $message = 'Request have failed on client side')
18+
{
19+
if ($response->getStatusCode() < 400 || $response->getStatusCode() >= 500) {
20+
throw new \InvalidArgumentException('Exception reserved to 4xx responses');
21+
}
22+
23+
parent::__construct($response, $message);
24+
}
25+
}

Exception/RequestFailedException.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of the CleverAge/OAuthApiBundle package.
4+
*
5+
* Copyright (C) 2017-2019 Clever-Age
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace CleverAge\OAuthApiBundle\Exception;
12+
13+
use Psr\Http\Message\ResponseInterface;
14+
15+
class RequestFailedException extends \RuntimeException
16+
{
17+
/** @var ResponseInterface */
18+
protected $response;
19+
20+
public function __construct(ResponseInterface $response, $message = 'Request have failed')
21+
{
22+
$message .= " (status code {$response->getStatusCode()})";
23+
24+
parent::__construct($message, 0, null);
25+
$this->response = $response;
26+
}
27+
28+
/**
29+
* @return ResponseInterface
30+
*/
31+
public function getResponse(): ResponseInterface
32+
{
33+
return $this->response;
34+
}
35+
36+
public static function createFromResponse(ResponseInterface $response): self
37+
{
38+
if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
39+
return new ClientRequestFailedException($response);
40+
} elseif ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
41+
return new ServerRequestFailedException($response);
42+
} else {
43+
return new RequestFailedException($response);
44+
}
45+
}
46+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of the CleverAge/OAuthApiBundle package.
4+
*
5+
* Copyright (C) 2017-2019 Clever-Age
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace CleverAge\OAuthApiBundle\Exception;
12+
13+
use Psr\Http\Message\ResponseInterface;
14+
15+
class ServerRequestFailedException extends RequestFailedException
16+
{
17+
public function __construct(ResponseInterface $response, $message = 'Request have failed on server side')
18+
{
19+
if ($response->getStatusCode() < 500 || $response->getStatusCode() >= 600) {
20+
throw new \InvalidArgumentException('Exception reserved to 5xx responses');
21+
}
22+
23+
parent::__construct($response, $message);
24+
}
25+
}

0 commit comments

Comments
 (0)