|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Happyr\ApiClient\Exception\Domain; |
|
4
|
|
|
|
|
5
|
|
|
use Happyr\ApiClient\Exception; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
final class HttpClientException extends \RuntimeException implements Exception |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var ResponseInterface|null |
|
15
|
|
|
*/ |
|
16
|
|
|
private $response; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $responseBody; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $message |
|
25
|
|
|
* @param int $code |
|
26
|
|
|
* @param ResponseInterface|null $response |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($message, $code, ResponseInterface $response = null) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct($message, $code); |
|
31
|
|
|
|
|
32
|
|
|
if ($response) { |
|
33
|
|
|
$this->response = $response; |
|
34
|
|
|
$body = $response->getBody()->__toString(); |
|
35
|
|
|
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) { |
|
36
|
|
|
$this->responseBody['message'] = $body; |
|
37
|
|
|
} else { |
|
38
|
|
|
$this->responseBody = json_decode($body, true); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
if (isset($this->responseBody['error'])) { |
|
41
|
|
|
$this->message .= "\n".$this->responseBody['error']['message']; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function badRequest(ResponseInterface $response = null) |
|
48
|
|
|
{ |
|
49
|
|
|
return new self('The parameters passed to the API were invalid. Check your inputs!', 400, $response); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function unauthorized(ResponseInterface $response = null) |
|
53
|
|
|
{ |
|
54
|
|
|
return new self('Your credentials are incorrect.', 401, $response); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function requestFailed(ResponseInterface $response = null) |
|
58
|
|
|
{ |
|
59
|
|
|
return new self('Parameters were valid but request failed. Try again.', 402, $response); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function notFound(ResponseInterface $response = null) |
|
63
|
|
|
{ |
|
64
|
|
|
return new self('The endpoint you tried to access does not exist. Check your URL.', 404, $response); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return ResponseInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getResponse() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->response; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getResponseBody() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->responseBody; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..