1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the zibios/wrike-php-guzzle package. |
5
|
|
|
* |
6
|
|
|
* (c) Zbigniew Ślązak |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zibios\WrikePhpGuzzle\Transformer\ApiException; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
15
|
|
|
use Zibios\WrikePhpLibrary\Exception\Api\ApiException; |
16
|
|
|
use Zibios\WrikePhpLibrary\Transformer\ApiException\AbstractApiExceptionTransformer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Guzzle Exception Transformer. |
20
|
|
|
*/ |
21
|
|
|
class GuzzleTransformer extends AbstractApiExceptionTransformer |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param \Exception $exception |
25
|
|
|
* |
26
|
|
|
* @return \Exception|ApiException |
27
|
|
|
*/ |
28
|
29 |
|
public function transform(\Exception $exception) |
29
|
|
|
{ |
30
|
29 |
|
if (false === $exception instanceof BadResponseException) { |
31
|
1 |
|
return new ApiException($exception); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
try { |
35
|
|
|
/** @var BadResponseException $exception */ |
36
|
28 |
|
$errorResponse = $exception->getResponse(); |
37
|
28 |
|
if (null === $errorResponse) { |
38
|
|
|
return new ApiException($exception); |
39
|
|
|
} |
40
|
28 |
|
$errorStatusCode = $errorResponse->getStatusCode(); |
41
|
28 |
|
$bodyString = (string) $errorResponse->getBody(); |
42
|
27 |
|
$bodyArray = json_decode($bodyString, true); |
43
|
27 |
|
$errorStatusName = ''; |
44
|
27 |
|
if (\is_array($bodyArray) && array_key_exists('error', $bodyArray)) { |
45
|
27 |
|
$errorStatusName = $bodyArray['error']; |
46
|
|
|
} |
47
|
1 |
|
} catch (\Exception $e) { |
48
|
1 |
|
return new ApiException($exception); |
49
|
|
|
} |
50
|
|
|
|
51
|
27 |
|
return $this->transformByStatusCodeAndName($exception, $errorStatusCode, $errorStatusName); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|