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|null |
39
|
|
|
*/ |
40
|
|
|
protected $errorCode; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int|null |
44
|
|
|
*/ |
45
|
|
|
protected $errorSubcode; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string|null |
49
|
|
|
*/ |
50
|
|
|
protected $errorMessage; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string|null |
54
|
|
|
*/ |
55
|
|
|
protected $errorUserTitle; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string|null |
59
|
|
|
*/ |
60
|
|
|
protected $errorUserMessage; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var int|null |
64
|
|
|
*/ |
65
|
|
|
protected $errorType; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array|null |
69
|
|
|
*/ |
70
|
|
|
protected $errorBlameFieldSpecs; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param ResponseInterface $response |
74
|
|
|
*/ |
75
|
8 |
|
public function __construct(ResponseInterface $response) { |
76
|
8 |
|
$this->response = $response; |
77
|
8 |
|
$error_data = static::getErrorData($response->getContent()); |
78
|
|
|
|
79
|
7 |
|
parent::__construct($error_data['message'], $error_data['code']); |
80
|
|
|
|
81
|
7 |
|
$this->errorSubcode = $error_data['error_subcode']; |
82
|
7 |
|
$this->errorUserTitle = $error_data['error_user_title']; |
83
|
7 |
|
$this->errorUserMessage = $error_data['error_user_msg']; |
84
|
7 |
|
$this->errorBlameFieldSpecs = $error_data['error_blame_field_specs']; |
85
|
7 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return ResponseInterface|null |
89
|
|
|
*/ |
90
|
|
|
public function getResponse() { |
91
|
|
|
return $this->response; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $array |
96
|
|
|
* @param string|int $key |
97
|
|
|
* @param mixed $default |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
7 |
|
protected static function idx(array $array, $key, $default = null) { |
101
|
7 |
|
return array_key_exists($key, $array) |
102
|
7 |
|
? $array[$key] |
103
|
7 |
|
: $default; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $response_data |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
7 |
|
protected static function getErrorData(array $response_data) { |
111
|
7 |
|
$error_data = static::idx($response_data, 'error', array()); |
112
|
|
|
|
113
|
|
|
return array( |
114
|
|
|
'code' => |
115
|
7 |
|
static::idx($error_data, 'code', static::idx($response_data, 'code')), |
116
|
7 |
|
'error_subcode' => static::idx($error_data, 'error_subcode'), |
117
|
7 |
|
'message' => static::idx($error_data, 'message'), |
118
|
7 |
|
'error_user_title' => static::idx($error_data, 'error_user_title'), |
119
|
7 |
|
'error_user_msg' => static::idx($error_data, 'error_user_msg'), |
120
|
|
|
'error_blame_field_specs' => |
121
|
7 |
|
static::idx(static::idx($error_data, 'error_data', array()), |
122
|
7 |
|
'blame_field_specs'), |
123
|
7 |
|
'type' => static::idx($error_data, 'type'), |
124
|
7 |
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Process an error payload from the Graph API and return the appropriate |
129
|
|
|
* exception subclass. |
130
|
|
|
* @param ResponseInterface $response |
131
|
|
|
* @return RequestException |
132
|
|
|
*/ |
133
|
6 |
|
public static function create(ResponseInterface $response) { |
134
|
6 |
|
$error_data = static::getErrorData($response->getContent()); |
135
|
6 |
|
if (in_array( |
136
|
6 |
|
$error_data['error_subcode'], array(458, 459, 460, 463, 464, 467)) |
137
|
6 |
|
|| in_array($error_data['code'], array(100, 102, 190)) |
138
|
6 |
|
|| $error_data['type'] === 'OAuthException') { |
139
|
|
|
|
140
|
1 |
|
return new AuthorizationException($response); |
141
|
5 |
|
} elseif (in_array($error_data['code'], array(1, 2))) { |
142
|
|
|
|
143
|
1 |
|
return new ServerException($response); |
144
|
4 |
|
} elseif (in_array($error_data['code'], array(4, 17, 341))) { |
145
|
|
|
|
146
|
1 |
|
return new ThrottleException($response); |
147
|
3 |
|
} elseif ($error_data['code'] == 506) { |
148
|
|
|
|
149
|
1 |
|
return new ClientException($response); |
150
|
2 |
|
} elseif ($error_data['code'] == 10 |
151
|
2 |
|
|| ($error_data['code'] >= 200 && $error_data['code'] <= 299)) { |
152
|
|
|
|
153
|
1 |
|
return new PermissionException($response); |
154
|
|
|
} else { |
155
|
|
|
|
156
|
1 |
|
return new self($response); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return int |
162
|
|
|
*/ |
163
|
1 |
|
public function getHttpStatusCode() { |
164
|
1 |
|
return $this->response->getStatusCode(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return int|null |
169
|
|
|
*/ |
170
|
1 |
|
public function getErrorSubcode() { |
171
|
1 |
|
return $this->errorSubcode; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string|null |
176
|
|
|
*/ |
177
|
1 |
|
public function getErrorUserTitle() { |
178
|
1 |
|
return $this->errorUserTitle; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return string|null |
183
|
|
|
*/ |
184
|
1 |
|
public function getErrorUserMessage() { |
185
|
1 |
|
return $this->errorUserMessage; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return array|null |
190
|
|
|
*/ |
191
|
1 |
|
public function getErrorBlameFieldSpecs() { |
192
|
1 |
|
return $this->errorBlameFieldSpecs; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return bool |
197
|
|
|
*/ |
198
|
|
|
public function isTransient() { |
199
|
|
|
if ($this->getResponse() !== null) { |
200
|
|
|
return false; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$body = $this->getResponse()->getBody(); |
|
|
|
|
204
|
|
|
|
205
|
|
|
return array_key_exists('error', $body) |
206
|
|
|
&& array_key_exists('is_transient', $body['error']) |
207
|
|
|
&& $body['error']['is_transient']; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.