1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stevenmaguire\Services\Trello\Exceptions; |
4
|
|
|
|
5
|
|
|
use Exception as BaseException; |
6
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
7
|
|
|
|
8
|
|
|
class Exception extends BaseException |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Response body |
12
|
|
|
* |
13
|
|
|
* @var object |
14
|
|
|
*/ |
15
|
|
|
protected $responseBody; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Creates local exception from http client exception, which attempts to |
19
|
|
|
* include response body. |
20
|
|
|
* |
21
|
|
|
* @param ClientExceptionInterface $clientException |
22
|
|
|
* |
23
|
|
|
* @return Exception |
24
|
|
|
*/ |
25
|
8 |
|
public static function fromClientException(ClientExceptionInterface $clientException) |
26
|
|
|
{ |
27
|
8 |
|
$response = static::getResponseFromClientException($clientException); |
28
|
8 |
|
$reason = $response ? $response->getReasonPhrase() : $clientException->getMessage(); |
|
|
|
|
29
|
8 |
|
$code = $response ? $response->getStatusCode() : $clientException->getCode(); |
|
|
|
|
30
|
8 |
|
$body = $response ? $response->getBody() : null; |
31
|
|
|
|
32
|
8 |
|
$exception = new static($reason, $code, $clientException); |
33
|
|
|
|
34
|
8 |
|
$json = json_decode($body); |
35
|
|
|
|
36
|
8 |
|
if (json_last_error() == JSON_ERROR_NONE) { |
37
|
4 |
|
return $exception->setResponseBody($json); |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
return $exception->setResponseBody($body); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Retrieves the response body property of exception. |
45
|
|
|
* |
46
|
|
|
* @return object |
47
|
|
|
*/ |
48
|
4 |
|
public function getResponseBody() |
49
|
|
|
{ |
50
|
4 |
|
return $this->responseBody; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Attempts to extract an http response message from the given client exception. |
55
|
|
|
* |
56
|
|
|
* @param ClientExceptionInterface $clientException |
57
|
|
|
* |
58
|
|
|
* @return \Psr\Http\Message\RequestInterface|null |
59
|
|
|
*/ |
60
|
8 |
|
public static function getResponseFromClientException(ClientExceptionInterface $clientException) |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
8 |
|
if (is_callable([$clientException, 'getResponse'])) { |
64
|
4 |
|
$response = $clientException->getResponse(); |
|
|
|
|
65
|
|
|
} else { |
66
|
8 |
|
throw new Exception('ClientException does not implement getResponse'); |
67
|
|
|
} |
68
|
4 |
|
} catch (Exception $e) { |
69
|
4 |
|
$response = null; |
70
|
|
|
} |
71
|
|
|
|
72
|
8 |
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Updates the response body property of exception. |
77
|
|
|
* |
78
|
|
|
* @param object |
79
|
|
|
* |
80
|
|
|
* @return Exception |
81
|
|
|
*/ |
82
|
8 |
|
public function setResponseBody($responseBody) |
83
|
|
|
{ |
84
|
8 |
|
$this->responseBody = $responseBody; |
85
|
|
|
|
86
|
8 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.