8000 Adding Exception Classes · dreamenemy/codebird-php@fd0b014 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd0b014

Browse files
authored
Adding Exception Classes
For better error logging and managing actions based up error type, use different exception types.
1 parent 2048e51 commit fd0b014

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/codebird.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ public function oauth_authenticate($force_login = NULL, $screen_name = NULL, $ty
10311031
throw new \Exception('To get the ' . $type . ' URL, use the correct third parameter, or omit it.');
10321032
}
10331033
if ($this->_oauth_token === null) {
1034-
throw new \Exception('To get the ' . $type . ' URL, the OAuth token must be set.');
1034+
throw new CodeBirdCredentialsException('To get the ' . $type . ' URL, the OAuth token must be set.');
10351035
}
10361036
$url = self::$_endpoints['oauth'] . 'oauth/' . $type . '?oauth_token=' . $this->_url($this->_oauth_token);
10371037
if ($force_login) {
@@ -1226,7 +1226,7 @@ private function _getProxyData($name)
12261226
protected function _oauth2TokenCurl()
12271227
{
12281228
if (self::$_consumer_key< 10000 /span> === null) {
1229-
throw new \Exception('To obtain a bearer token, the consumer key must be set.');
1229+
throw new CodeBirdCredentialsException('To obtain a bearer token, the consumer key must be set.');
12301230
}
12311231
$post_fields = [
12321232
'grant_type' => 'client_credentials'
@@ -1244,7 +1244,7 @@ protected function _oauth2TokenCurl()
12441244

12451245
// catch request errors
12461246
if ($result === false) {
1247-
throw new \Exception('Request error for bearer token: ' . $this->_curl_error($connection));
1247+
throw new CodeBirdAuthException('Request error for bearer token: ' . $this->_curl_error($connection));
12481248
}
12491249

12501250
// certificate validation results
@@ -1266,14 +1266,14 @@ protected function _oauth2TokenCurl()
12661266
protected function _oauth2TokenNoCurl()
12671267
{
12681268
if (self::$_consumer_key == null) {
1269-
throw new \Exception('To obtain a bearer token, the consumer key must be set.');
1269+
throw new CodeBirdCredentialsException('To obtain a bearer token, the consumer key must be set.');
12701270
}
12711271

12721272
$url = self::$_endpoints['oauth'] . 'oauth2/token';
12731273
$hostname = parse_url($url, PHP_URL_HOST);
12741274

12751275
if ($hostname === false) {
1276-
throw new \Exception('Incorrect API endpoint host.');
1276+
throw new CodeBirdEndpointException('Incorrect API endpoint host.');
12771277
}
12781278

12791279
$contextOptions = [
@@ -1465,7 +1465,7 @@ protected function _url($data)
14651465
protected function _sha1($data)
14661466
{
14671467
if (self::$_consumer_secret === null) {
1468-
throw new \Exception('To generate a hash, the consumer secret must be set.');
1468+
throw new CodeBirdCredentialsException('To generate a hash, the consumer secret must be set.');
14691469
}
14701470
if (!function_exists('hash_hmac')) {
14711471
throw new \Exception('To generate a hash, the PHP hash extension must be available.');
@@ -1540,7 +1540,7 @@ protected function _getSignature($httpmethod, $method, $base_params)
15401540
protected function _sign($httpmethod, $method, $params = [])
15411541
{
15421542
if (self::$_consumer_key === null) {
1543-
throw new \Exception('To generate a signature, the consumer key must be set.');
1543+
throw new CodeBirdCredentialsException('To generate a signature, the consumer key must be set.');
15441544
}
15451545
$sign_base_params = array_map(
15461546
[$this, '_url'],
@@ -1743,7 +1743,7 @@ protected function _getMultipartRequestFromParams($method_template, $border, $pa
17431743
foreach ($params as $key => $value) {
17441744
// is it an array?
17451745
if (is_array($value)) {
1746-
throw new \Exception('Using URL-encoded parameters is not supported for uploading media.');
1746+
throw new CodeBirdMediaException('Using URL-encoded parameters is not supported for uploading media.');
17471747
}
17481748
$request .=
17491749
'--' . $border . "\r\n"
@@ -1874,7 +1874,7 @@ protected function _fetchRemoteFile($url)
18741874
) {
18751875
return $result;
18761876
}
1877-
throw new \Exception('Downloading a remote media file failed.');
1877+
throw new CodeBirdMediaException('Downloading a remote media file failed.');
18781878
return 8000 false;
18791879
}
18801880
// no cURL
@@ -1894,7 +1894,7 @@ protected function _fetchRemoteFile($url)
18941894
) {
18951895
return $result;
18961896
}
1897-
throw new \Exception('Downloading a remote media file failed.');
1897+
throw new CodeBirdMediaException('Downloading a remote media file failed.');
18981898
return false;
18991899
}
19001900

@@ -2025,7 +2025,7 @@ protected function _callApi($httpmethod, $method, $method_template, $params = []
20252025
&& $this->_oauth_token === null
20262026
&& substr($method, 0, 5) !== 'oauth'
20272027
) {
2028-
throw new \Exception('To call this API, the OAuth access token must be set.');
2028+
throw new CodeBirdCredentialsException('To call this API, the OAuth access token must be set.');
20292029
}
20302030
// use separate API access for streaming API
20312031
if ($this->_detectStreaming($method) !== false) {
@@ -2124,7 +2124,7 @@ protected function _callApiNoCurl(
21242124

21252125
$hostname = parse_url($url, PHP_URL_HOST);
21262126
if ($hostname === false) {
2127-
throw new \Exception('Incorrect API endpoint host.');
2127+
throw new CodeBirdEndpointException('Incorrect API endpoint host.');
21282128
}
21292129

21302130
$request_headers[] = 'Authorization: ' . $authorization;
@@ -2296,7 +2296,7 @@ protected function _getBearerAuthorization()
22962296
if (self::$_consumer_key === null
22972297
&& self::$_bearer_token === null
22982298
) {
2299-
throw new \Exception('To make an app-only auth API request, consumer key or bearer token must be set.');
2299+
throw new CodeBirdCredentialsException('To make an app-only auth API request, consumer key or bearer token must be set.');
23002300
}
23012301
// automatically fetch bearer token, if necessary
23022302
if (self::$_bearer_token === null) {
@@ -2373,7 +2373,7 @@ protected function _callApiStreaming(
23732373
$path = parse_url($url, PHP_URL_PATH);
23742374
$query = parse_url($url, PHP_URL_QUERY);
23752375
if ($hostname === false) {
2376-
throw new \Exception('Incorrect API endpoint host.');
2376+
throw new CodeBirdEndpointException('Incorrect API endpoint host.');
23772377
}
23782378

23792379
$request_headers[] = 'Authorization: ' . $authorization;
@@ -2635,4 +2635,36 @@ protected function _parseApiReply($reply)
26352635
}
26362636
return $parsed;
26372637
}
2638+
2639+
}
2640+
2641+
/**
2642+
* Catch errors when authtoken is expired
2643+
*/
2644+
class CodeBirdAuthException extends \Exception {
2645+
2646+
}
2647+
2648+
2649+
/**
2650+
* Catch error when credentials are not set correclty
2651+
*/
2652+
class CodeBirdCredentialsException extends \Exception {
2653+
26382654
}
2655+
2656+
/**
2657+
* Catch errors r elated to bad endpoi ts
2658+
*/
2659+
class CodeBirdEndpointException extends \Exception {
2660+
2661+
}
2662+
2663+
/*
2664+
* Catch errors relatedto media
2665+
*/
2666+
2667+
class CodeBirdMediaException extends \Exception {
2668+
2669+
}
2670+

0 commit comments

Comments
 (0)
0