1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
7
|
|
|
* form for use in connection with the web services and APIs provided by |
8
|
|
|
* Facebook. |
9
|
|
|
* |
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace FacebookAds\Http\Exception; |
26
|
|
|
|
27
|
|
|
use FacebookAds\Exception\Exception; |
28
|
|
|
use FacebookAds\Http\ResponseInterface; |
29
|
|
|
|
30
|
|
|
class RequestException extends Exception { |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ResponseInterface|null |
34
|
|
|
*/ |
35
|
|
|
protected $response; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int Status code for the response causing the exception |
39
|
|
|
*/ |
40
|
|
|
protected $statusCode; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int|null |
44
|
|
|
*/ |
45
|
|
|
protected $errorCode; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int|null |
49
|
|
|
*/ |
50
|
|
|
protected $errorSubcode; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string|null |
54
|
|
|
*/ |
55
|
|
|
protected $errorMessage; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string|null |
59
|
|
|
*/ |
60
|
|
|
protected $errorUserTitle; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string|null |
64
|
|
|
*/ |
65
|
|
|
protected $errorUserMessage; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var int|null |
69
|
|
|
*/ |
70
|
|
|
protected $errorType; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var array|null |
74
|
|
|
*/ |
75
|
|
|
protected $errorBlameFieldSpecs; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* FIXME - v2.6 breaking change: |
79
|
|
|
* make interface __contruct(ResponseInterface $response) |
80
|
|
|
* |
81
|
|
|
* @param array $response_data The response from the Graph API |
82
|
|
|
* @param int $status_code |
83
|
|
|
*/ |
84
|
8 |
|
public function __construct( |
85
|
|
|
array $response_data, |
86
|
|
|
$status_code) { |
87
|
|
|
|
88
|
8 |
|
$this->statusCode = $status_code; |
89
|
8 |
|
$error_data = static::getErrorData($response_data); |
90
|
|
|
|
91
|
8 |
|
parent::__construct($error_data['message'], $error_data['code']); |
92
|
|
|
|
93
|
8 |
|
$this->errorSubcode = $error_data['error_subcode']; |
94
|
8 |
|
$this->errorUserTitle = $error_data['error_user_title']; |
95
|
8 |
|
$this->errorUserMessage = $error_data['error_user_msg']; |
96
|
8 |
|
$this->errorBlameFieldSpecs = $error_data['error_blame_field_specs']; |
97
|
8 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return ResponseInterface|null |
101
|
|
|
*/ |
102
|
|
|
public function getResponse() { |
103
|
|
|
return $this->response; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param ResponseInterface $response |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
1 |
|
public function setResponse(ResponseInterface $response) { |
111
|
1 |
|
$this->response = $response; |
112
|
|
|
|
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $array |
118
|
|
|
* @param string|int $key |
119
|
|
|
* @param mixed $default |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
8 |
|
protected static function idx(array $array, $key, $default = null) { |
123
|
8 |
|
return array_key_exists($key, $array) |
124
|
8 |
|
? $array[$key] |
125
|
8 |
|
: $default; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param array $response_data |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
8 |
|
protected static function getErrorData(array $response_data) { |
133
|
8 |
|
$error_data = static::idx($response_data, 'error', array()); |
134
|
|
|
|
135
|
|
|
return array( |
136
|
|
|
'code' => |
137
|
8 |
|
static::idx($error_data, 'code', static::idx($response_data, 'code')), |
138
|
8 |
|
'error_subcode' => static::idx($error_data, 'error_subcode'), |
139
|
8 |
|
'message' => static::idx($error_data, 'message'), |
140
|
8 |
|
'error_user_title' => static::idx($error_data, 'error_user_title'), |
141
|
8 |
|
'error_user_msg' => static::idx($error_data, 'error_user_msg'), |
142
|
|
|
'error_blame_field_specs' => |
143
|
8 |
|
static::idx(static::idx($error_data, 'error_data', array()), |
144
|
8 |
|
'blame_field_specs'), |
145
|
8 |
|
'type' => static::idx($error_data, 'type'), |
146
|
8 |
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Process an error payload from the Graph API and return the appropriate |
151
|
|
|
* exception subclass. |
152
|
|
|
* @param array $response_data the decoded response from the Graph API |
153
|
|
|
* @param int $status_code the HTTP response code |
154
|
|
|
* @return RequestException |
155
|
|
|
*/ |
156
|
7 |
|
public static function create(array $response_data, $status_code) { |
157
|
7 |
|
$error_data = static::getErrorData($response_data); |
158
|
7 |
|
if (in_array( |
159
|
7 |
|
$error_data['error_subcode'], array(458, 459, 460, 463, 464, 467)) |
160
|
7 |
|
|| in_array($error_data['code'], array(100, 102, 190)) |
161
|
7 |
|
|| $error_data['type'] === 'OAuthException') { |
162
|
|
|
|
163
|
1 |
|
return new AuthorizationException($response_data, $status_code); |
164
|
6 |
|
} elseif (in_array($error_data['code'], array(1, 2))) { |
165
|
|
|
|
166
|
2 |
|
return new ServerException($response_data, $status_code); |
167
|
4 |
|
} elseif (in_array($error_data['code'], array(4, 17, 341))) { |
168
|
|
|
|
169
|
1 |
|
return new ThrottleException($response_data, $status_code); |
170
|
3 |
|
} elseif ($error_data['code'] == 506) { |
171
|
|
|
|
172
|
1 |
|
return new ClientException($response_data, $status_code); |
173
|
2 |
|
} elseif ($error_data['code'] == 10 |
174
|
2 |
|
|| ($error_data['code'] >= 200 && $error_data['code'] <= 299)) { |
175
|
|
|
|
176
|
1 |
|
return new PermissionException($response_data, $status_code); |
177
|
|
|
} else { |
178
|
|
|
|
179
|
1 |
|
return new self($response_data, $status_code); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return int |
185
|
|
|
*/ |
186
|
1 |
|
public function getHttpStatusCode() { |
187
|
1 |
|
return $this->statusCode; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return int|null |
192
|
|
|
*/ |
193
|
1 |
|
public function getErrorSubcode() { |
194
|
1 |
|
return $this->errorSubcode; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return string|null |
199
|
|
|
*/ |
200
|
1 |
|
public function getErrorUserTitle() { |
201
|
1 |
|
return $this->errorUserTitle; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return string|null |
206
|
|
|
*/ |
207
|
1 |
|
public function getErrorUserMessage() { |
208
|
1 |
|
return $this->errorUserMessage; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return array|null |
213
|
|
|
*/ |
214
|
1 |
|
public function getErrorBlameFieldSpecs() { |
215
|
1 |
|
return $this->errorBlameFieldSpecs; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
|
|
public function isTransient() { |
222
|
|
|
if ($this->getResponse() !== null) { |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$body = $this->getResponse()->getBody(); |
|
|
|
|
227
|
|
|
|
228
|
|
|
return array_key_exists('error', $body) |
229
|
|
|
&& array_key_exists('is_transient', $body['error']) |
230
|
|
|
&& $body['error']['is_transient']; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.