-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathHttpClientException.php
More file actions
152 lines (129 loc) · 3.36 KB
/
Copy pathHttpClientException.php
File metadata and controls
152 lines (129 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace CloudConvert\Exceptions;
use Psr\Http\Message\ResponseInterface;
class HttpClientException extends Exception
{
/**
* @var ResponseInterface|null
*/
protected $response;
/**
* @var array
*/
protected $responseBody;
/**
* @var int
*/
protected $responseCode;
/**
* @var string|null
*/
protected $errorCode;
/**
* HttpClientException constructor.
*
* @param string $message
* @param int $code
* @param ResponseInterface $response
*/
public function __construct(string $message, int $code, ResponseInterface $response)
{
$this->response = $response;
$this->responseCode = $response->getStatusCode();
$this->responseBody = @json_decode($response->getBody(), true) ?? [];
$this->errorCode = isset($this->responseBody['code']) ? $this->responseBody['code'] : null;
if (isset($this->responseBody['message'])) {
$message = $this->responseBody['message'];
}
if (isset($this->getResponseBody()['errors'])) {
$message = $this->getMessage();
foreach ($this->getResponseBody()['errors'] as $field => $errors) {
$message .= ' ' . $field . ': ' . implode(' ', $errors);
}
}
parent::__construct($message, $code);
}
/**
* @param ResponseInterface $response
*
* @return HttpClientException
*/
public static function badRequest(ResponseInterface $response)
{
return new self('Invalid data.', 400, $response);
}
/**
* @param ResponseInterface $response
*
* @return HttpClientException
*/
public static function unauthorized(ResponseInterface $response)
{
return new self('Unauthorized.', 401, $response);
}
/**
* @param ResponseInterface $response
*
* @return HttpClientException
*/
public static function paymentRequired(ResponseInterface $response)
{
return new self('Credits used up.', 402, $response);
}
/**
* @param ResponseInterface $response
*
* @return HttpClientException
*/
public static function forbidden(ResponseInterface $response)
{
return new self('Forbidden.', 403, $response);
}
/**
* @param ResponseInterface $response
*
* @return HttpClientException
*/
public static function unprocessable(ResponseInterface $response)
{
return new self('Unprocessable.', 422, $response);
}
/**
* @param ResponseInterface $response
*
* @return HttpClientException
*/
public static function notFound(ResponseInterface $response)
{
return new self('The endpoint you have tried to access does not exist. ',
404, $response);
}
/**
* @return ResponseInterface|null
*/
public function getResponse(): ?ResponseInterface
{
return $this->response;
}
/**
* @return array
*/
public function getResponseBody(): array
{
return $this->responseBody;
}
/**
* @return int
*/
public function getResponseCode(): int
{
return $this->responseCode;
}
/**
* @return string|null
*/
public function getErrorCode(): ?string
{
return $this->errorCode;
}
}