|
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
|
|
|
public function __construct(ResponseInterface $response) { |
|
76
|
|
|
$this->response = $response; |
|
77
|
|
|
$error_data = static::getErrorData($response->getContent()); |
|
78
|
|
|
|
|
79
|
|
|
parent::__construct($error_data['message'], $error_data['code']); |
|
80
|
|
|
|
|
81
|
|
|
$this->errorSubcode = $error_data['error_subcode']; |
|
82
|
|
|
$this->errorUserTitle = $error_data['error_user_title']; |
|
83
|
|
|
$this->errorUserMessage = $error_data['error_user_msg']; |
|
84
|
8 |
|
$this->errorBlameFieldSpecs = $error_data['error_blame_field_specs']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
8 |
|
* @return ResponseInterface|null |
|
89
|
8 |
|
*/ |
|
90
|
|
|
public function getResponse() { |
|
91
|
8 |
|
return $this->response; |
|
92
|
|
|
} |
|
93
|
8 |
|
|
|
94
|
8 |
|
/** |
|
95
|
8 |
|
* @param array $array |
|
96
|
8 |
|
* @param string|int $key |
|
97
|
8 |
|
* @param mixed $default |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
protected static function idx(array $array, $key, $default = null) { |
|
101
|
|
|
return array_key_exists($key, $array) |
|
102
|
|
|
? $array[$key] |
|
103
|
|
|
: $default; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param array $response_data |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
1 |
|
protected static function getErrorData(array $response_data) { |
|
111
|
1 |
|
$error_data = static::idx($response_data, 'error', array()); |
|
112
|
|
|
|
|
113
|
1 |
|
return array( |
|
114
|
|
|
'code' => |
|
115
|
|
|
static::idx($error_data, 'code', static::idx($response_data, 'code')), |
|
116
|
|
|
'error_subcode' => static::idx($error_data, 'error_subcode'), |
|
117
|
|
|
'message' => static::idx($error_data, 'message'), |
|
118
|
|
|
'error_user_title' => static::idx($error_data, 'error_user_title'), |
|
119
|
|
|
'error_user_msg' => static::idx($error_data, 'error_user_msg'), |
|
120
|
|
|
'error_blame_field_specs' => |
|
121
|
|
|
static::idx(static::idx($error_data, 'error_data', array()), |
|
122
|
8 |
|
'blame_field_specs'), |
|
123
|
8 |
|
'type' => static::idx($error_data, 'type'), |
|
124
|
8 |
|
); |
|
125
|
8 |
|
} |
|
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
|
8 |
|
*/ |
|
133
|
8 |
|
public static function create(ResponseInterface $response) { |
|
134
|
|
|
$error_data = static::getErrorData($response->getContent()); |
|
135
|
|
|
if (in_array( |
|
136
|
|
|
$error_data['error_subcode'], array(458, 459, 460, 463, 464, 467)) |
|
137
|
8 |
|
|| in_array($error_data['code'], array(102, 190))) { |
|
138
|
8 |
|
|
|
139
|
8 |
|
return new AuthorizationException($response); |
|
140
|
8 |
|
} elseif (in_array($error_data['code'], array(1, 2))) { |
|
141
|
8 |
|
|
|
142
|
|
|
return new ServerException($response); |
|
143
|
8 |
|
} elseif (in_array($error_data['code'], array(4, 17, 341))) { |
|
144
|
8 |
|
|
|
145
|
8 |
|
return new ThrottleException($response); |
|
146
|
8 |
|
} elseif ($error_data['code'] == 506) { |
|
147
|
|
|
|
|
148
|
|
|
return new ClientException($response); |
|
149
|
|
|
} elseif ($error_data['code'] == 10 |
|
150
|
|
|
|| ($error_data['code'] >= 200 && $error_data['code'] <= 299)) { |
|
151
|
|
|
|
|
152
|
|
|
return new PermissionException($response); |
|
153
|
|
|
} else { |
|
154
|
|
|
return new self($response); |
|
155
|
|
|
} |
|
156
|
7 |
|
} |
|
157
|
7 |
|
|
|
158
|
7 |
|
/** |
|
159
|
7 |
|
* @return int |
|
160
|
7 |
|
*/ |
|
161
|
7 |
|
public function getHttpStatusCode() { |
|
162
|
|
|
return $this->response->getStatusCode(); |
|
163
|
1 |
|
} |
|
164
|
6 |
|
|
|
165
|
|
|
/** |
|
166
|
2 |
|
* @return int|null |
|
167
|
4 |
|
*/ |
|
168
|
|
|
public function getErrorSubcode() { |
|
169
|
1 |
|
return $this->errorSubcode; |
|
170
|
3 |
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
/** |
|
173
|
2 |
|
* @return string|null |
|
174
|
2 |
|
*/ |
|
175
|
|
|
public function getErrorUserTitle() { |
|
176
|
1 |
|
return $this->errorUserTitle; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
1 |
|
/** |
|
180
|
|
|
* @return string|null |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getErrorUserMessage() { |
|
183
|
|
|
return $this->errorUserMessage; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
1 |
|
/** |
|
187
|
1 |
|
* @return array|null |
|
188
|
|
|
*/ |
|
189
|
|
|
public function getErrorBlameFieldSpecs() { |
|
190
|
|
|
return $this->errorBlameFieldSpecs; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
/** |
|
194
|
1 |
|
* @return bool |
|
195
|
|
|
*/ |
|
196
|
|
|
public function isTransient() { |
|
197
|
|
|
if ($this->getResponse() !== null) { |
|
198
|
|
|
return false; |
|
199
|
|
|
} |
|
200
|
1 |
|
|
|
201
|
1 |
|
$body = $this->getResponse()->getBody(); |
|
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
return array_key_exists('error', $body) |
|
204
|
|
|
&& array_key_exists('is_transient', $body['error']) |
|
205
|
|
|
&& $body['error']['is_transient']; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.