diff --git a/src/codebird.php b/src/codebird.php index f4e8771..146d9b2 100644 --- a/src/codebird.php +++ b/src/codebird.php @@ -1045,7 +1045,7 @@ public function oauth_authenticate($force_login = NULL, $screen_name = NULL, $ty throw new \Exception('To get the ' . $type . ' URL, use the correct third parameter, or omit it.'); } if ($this->_oauth_token === null) { - throw new \Exception('To get the ' . $type . ' URL, the OAuth token must be set.'); + throw new CodebirdCredentialsException('To get the ' . $type . ' URL, the OAuth token must be set.'); } $url = self::$_endpoints['oauth'] . 'oauth/' . $type . '?oauth_token=' . $this->_url($this->_oauth_token); if ($force_login) { @@ -1240,7 +1240,7 @@ private function _getProxyData($name) protected function _oauth2TokenCurl() { if (self::$_consumer_key === null) { - throw new \Exception('To obtain a bearer token, the consumer key must be set.'); + throw new CodebirdCredentialsException('To obtain a bearer token, the consumer key must be set.'); } $post_fields = [ 'grant_type' => 'client_credentials' @@ -1258,7 +1258,7 @@ protected function _oauth2TokenCurl() // catch request errors if ($result === false) { - throw new \Exception('Request error for bearer token: ' . $this->_curl_error($connection)); + throw new CodebirdAuthException('Request error for bearer token: ' . $this->_curl_error($connection)); } // certificate validation results @@ -1280,14 +1280,14 @@ protected function _oauth2TokenCurl() protected function _oauth2TokenNoCurl() { if (self::$_consumer_key == null) { - throw new \Exception('To obtain a bearer token, the consumer key must be set.'); + throw new CodebirdCredentialsException('To obtain a bearer token, the consumer key must be set.'); } $url = self::$_endpoints['oauth'] . 'oauth2/token'; $hostname = parse_url($url, PHP_URL_HOST); if ($hostname === false) { - throw new \Exception('Incorrect API endpoint host.'); + throw new CodebirdEndpointException('Incorrect API endpoint host.'); } $contextOptions = [ @@ -1479,7 +1479,7 @@ protected function _url($data) protected function _sha1($data) { if (self::$_consumer_secret === null) { - throw new \Exception('To generate a hash, the consumer secret must be set.'); + throw new CodebirdCredentialsException('To generate a hash, the consumer secret must be set.'); } if (!function_exists('hash_hmac')) { throw new \Exception('To generate a hash, the PHP hash extension must be available.'); @@ -1554,7 +1554,7 @@ protected function _getSignature($httpmethod, $method, $base_params) protected function _sign($httpmethod, $method, $params = []) { if (self::$_consumer_key === null) { - throw new \Exception('To generate a signature, the consumer key must be set.'); + throw new CodebirdCredentialsException('To generate a signature, the consumer key must be set.'); } $sign_base_params = array_map( [$this, '_url'], @@ -1757,7 +1757,7 @@ protected function _getMultipartRequestFromParams($method_template, $border, $pa foreach ($params as $key => $value) { // is it an array? if (is_array($value)) { - throw new \Exception('Using URL-encoded parameters is not supported for uploading media.'); + throw new CodebirdMediaException('Using URL-encoded parameters is not supported for uploading media.'); } $request .= '--' . $border . "\r\n" @@ -1888,7 +1888,7 @@ protected function _fetchRemoteFile($url) ) { return $result; } - throw new \Exception('Downloading a remote media file failed.'); + throw new CodebirdMediaException('Downloading a remote media file failed.'); return false; } // no cURL @@ -1908,7 +1908,7 @@ protected function _fetchRemoteFile($url) ) { return $result; } - throw new \Exception('Downloading a remote media file failed.'); + throw new CodebirdMediaException('Downloading a remote media file failed.'); return false; } @@ -2039,7 +2039,7 @@ protected function _callApi($httpmethod, $method, $method_template, $params = [] && $this->_oauth_token === null && substr($method, 0, 5) !== 'oauth' ) { - throw new \Exception('To call this API, the OAuth access token must be set.'); + throw new CodebirdCredentialsException('To call this API, the OAuth access token must be set.'); } // use separate API access for streaming API if ($this->_detectStreaming($method) !== false) { @@ -2138,7 +2138,7 @@ protected function _callApiNoCurl( $hostname = parse_url($url, PHP_URL_HOST); if ($hostname === false) { - throw new \Exception('Incorrect API endpoint host.'); + throw new CodebirdEndpointException('Incorrect API endpoint host.'); } $request_headers[] = 'Authorization: ' . $authorization; @@ -2310,7 +2310,7 @@ protected function _getBearerAuthorization() if (self::$_consumer_key === null && self::$_bearer_token === null ) { - throw new \Exception('To make an app-only auth API request, consumer key or bearer token must be set.'); + throw new CodebirdCredentialsException('To make an app-only auth API request, consumer key or bearer token must be set.'); } // automatically fetch bearer token, if necessary if (self::$_bearer_token === null) { @@ -2387,7 +2387,7 @@ protected function _callApiStreaming( $path = parse_url($url, PHP_URL_PATH); $query = parse_url($url, PHP_URL_QUERY); if ($hostname === false) { - throw new \Exception('Incorrect API endpoint host.'); + throw new CodebirdEndpointException('Incorrect API endpoint host.'); } $request_headers[] = 'Authorization: ' . $authorization; @@ -2649,4 +2649,36 @@ protected function _parseApiReply($reply) } return $parsed; } + +} + +/** + * Catch errors when authtoken is expired + */ +class CodebirdAuthException extends \Exception { + +} + + +/** + * Catch error when credentials are not set correclty + */ +class CodebirdCredentialsException extends \Exception { + } + +/** + * Catch errors r elated to bad endpoi ts + */ +class CodebirdEndpointException extends \Exception { + +} + +/* + * Catch errors relatedto media + */ + +class CodebirdMediaException extends \Exception { + +} +