1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the WrikePhpSdk package. |
4
|
|
|
* |
5
|
|
|
* (c) Zbigniew Ślązak |
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 Zibios\WrikePhpSdk\Exceptions\Api; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use GuzzleHttp\Exception\ClientException; |
15
|
|
|
use GuzzleHttp\Exception\ServerException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* General Wrike Api Exception |
19
|
|
|
* |
20
|
|
|
* Thrown when the Wrike API returns an HTTP error that isn't handled by other dedicated exceptions. |
21
|
|
|
*/ |
22
|
|
|
class ApiException extends Exception |
23
|
|
|
{ |
24
|
|
|
/** @var \Exception */ |
25
|
|
|
protected $responseException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ApiException constructor. |
29
|
|
|
* |
30
|
|
|
* @param Exception $e |
31
|
|
|
*/ |
32
|
28 |
|
public function __construct(\Exception $e) |
33
|
|
|
{ |
34
|
28 |
|
parent::__construct($e->getMessage(), $e->getCode(), $e); |
35
|
28 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \Exception $e |
39
|
|
|
* |
40
|
|
|
* @return ApiException |
41
|
|
|
*/ |
42
|
18 |
|
public static function create(\Exception $e) |
43
|
|
|
{ |
44
|
18 |
|
if ($e instanceof ClientException === false && $e instanceof ServerException === false) { |
45
|
1 |
|
return new ApiException($e); |
46
|
|
|
} |
47
|
|
|
/** @var ClientException $e */ |
48
|
17 |
|
$errorResponse = $e->getResponse(); |
|
|
|
|
49
|
17 |
|
$bodyString = (string) $errorResponse->getBody(); |
|
|
|
|
50
|
17 |
|
$body = json_decode($bodyString, true); |
|
|
|
|
51
|
17 |
|
$errorStatusCode = $errorResponse->getStatusCode(); |
52
|
17 |
|
$errorName = $body['error']; |
|
|
|
|
53
|
|
|
|
54
|
17 |
|
switch ($errorStatusCode . $errorName) { |
55
|
17 |
|
case (AccessForbiddenException::STATUS_CODE . AccessForbiddenException::STATUS_NAME): |
56
|
1 |
|
return new AccessForbiddenException($e); |
57
|
16 |
|
case (InvalidParameterException::STATUS_CODE . InvalidParameterException::STATUS_NAME): |
58
|
1 |
|
return new InvalidParameterException($e); |
59
|
15 |
|
case (InvalidRequestException::STATUS_CODE . InvalidRequestException::STATUS_NAME): |
60
|
1 |
|
return new InvalidRequestException($e); |
61
|
14 |
|
case (MethodNotFoundException::STATUS_CODE . MethodNotFoundException::STATUS_NAME): |
62
|
1 |
|
return new MethodNotFoundException($e); |
63
|
13 |
|
case (NotAllowedException::STATUS_CODE . NotAllowedException::STATUS_NAME): |
64
|
1 |
|
return new NotAllowedException($e); |
65
|
12 |
|
case (NotAuthorizedException::STATUS_CODE . NotAuthorizedException::STATUS_NAME): |
66
|
1 |
|
return new NotAuthorizedException($e); |
67
|
11 |
|
case (ParameterRequiredException::STATUS_CODE . ParameterRequiredException::STATUS_NAME): |
68
|
1 |
|
return new ParameterRequiredException($e); |
69
|
10 |
|
case (ResourceNotFoundException::STATUS_CODE . ResourceNotFoundException::STATUS_NAME): |
70
|
1 |
|
return new ResourceNotFoundException($e); |
71
|
9 |
|
case (ServerErrorException::STATUS_CODE . ServerErrorException::STATUS_NAME): |
72
|
1 |
|
return new ServerErrorException($e); |
73
|
8 |
|
default: |
74
|
8 |
|
return new ApiException($e); |
75
|
8 |
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.