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