Skip to content

Commit

Permalink
Fix backwards compatibility issue with BaseObject extension. Enhance …
Browse files Browse the repository at this point in the history
…error messages with code data. Documentation: add all returned exceptions to the functions documentation.
  • Loading branch information
drsdre committed Dec 18, 2017
1 parent 21f38f5 commit 339d716
Showing 1 changed file with 46 additions and 16 deletions.
62 changes: 46 additions & 16 deletions Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use yii\helpers\Json;
use yii\authclient\OAuthToken;
use yii\base\BaseObject;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\httpclient\Request;
Expand All @@ -22,7 +23,7 @@
*
* @author Andre Schuurman <[email protected]>
*/
class Client extends \yii\base\Object {
class Client extends BaseObject {

/**
* @var string API url endpoint
Expand Down Expand Up @@ -149,6 +150,9 @@ public function init() {
* @param array $request_data
*
* @return $this
* @throws Exception
* @throws \Exception
* @throws \yii\httpclient\Exception
*/
public function getData(
$entity_url,
Expand Down Expand Up @@ -185,6 +189,9 @@ public function getData(
* @param array $update_data
*
* @return $this
* @throws Exception
* @throws \Exception
* @throws \yii\httpclient\Exception
*/
public function putData(
$entity_url,
Expand Down Expand Up @@ -214,6 +221,9 @@ public function putData(
* @param array $update_data
*
* @return $this
* @throws Exception
* @throws \Exception
* @throws \yii\httpclient\Exception
*/
public function patchData(
$entity_url,
Expand Down Expand Up @@ -245,6 +255,9 @@ public function patchData(
* @param array $update_data
*
* @return $this
* @throws Exception
* @throws \Exception
* @throws \yii\httpclient\Exception
*/
public function postData(
$entity_url,
Expand Down Expand Up @@ -273,6 +286,9 @@ public function postData(
* @param bool $force
*
* @return $this
* @throws Exception
* @throws \Exception
* @throws \yii\httpclient\Exception
*/
public function deleteData(
string $entity_url,
Expand Down Expand Up @@ -301,6 +317,9 @@ public function deleteData(
* @param $file_data
*
* @return $this
* @throws Exception
* @throws \Exception
* @throws \yii\httpclient\Exception
*/
public function uploadFile(
$entity_url,
Expand Down Expand Up @@ -330,7 +349,7 @@ public function uploadFile(
* Return content as array
*
* @return array
* @throws Exception
* @throws \Exception
*/
public function asArray() {
if ( isset( $this->response->content ) ) {
Expand All @@ -344,7 +363,7 @@ public function asArray() {
* Return content as object
*
* @return \stdClass
* @throws Exception
* @throws \Exception
*/
public function asObject() {
if ( isset( $this->response->content ) ) {
Expand Down Expand Up @@ -414,7 +433,6 @@ protected function createAuthenticatedRequest() {
* Parse API response
*
* @throws Exception on failure
* @throws \yii\httpclient\Exception
* @throws \Exception
*/
protected function executeRequest() {
Expand All @@ -434,6 +452,9 @@ protected function executeRequest() {
// Check if response is valid
if ( ! $this->response->isOk ) {

$error_data = (isset($result_content->code)?' Code: ' . $result_content->code . ' ':'') .
' URL: ' . $this->request->getFullUrl();

// Error handling
switch ( $this->response->statusCode ) {
case 304:
Expand All @@ -450,7 +471,7 @@ protected function executeRequest() {
throw new Exception(
'Bad Request ' . (isset($result_content->message)?$result_content->message:'unknown') .
' Params: ' .implode( ' | ', $parameter_errors ) .
' URL: ' . $this->request->getFullUrl(),
$error_data,
$this->response->statusCode
);
case 401:
Expand All @@ -460,44 +481,52 @@ protected function executeRequest() {
// Map to status code 432 (unassigned)
throw new Exception(
( isset( $result_content->message ) ? $result_content->message : $this->response->content ) .
' ' . $this->request->getFullUrl(),
$error_data,
432
);
}

// Generic 401 error
throw new Exception(
'Unauthorized: ' . $result_content->message . ' (request ' . $this->request->getFullUrl() . ').',
'Unauthorized: ' .
( isset( $result_content->message ) ? $result_content->message : $this->response->content ) .
$error_data,
$this->response->statusCode
);
case 403:
throw new Exception(
'Forbidden: request not allowed accessing ' . $this->request->getFullUrl(),
'Forbidden: request not allowed.' .
$error_data,
$this->response->statusCode
);
case 404:
throw new Exception(
'Not found: ' . $this->request->getFullUrl() . ' does not exist.',
'Not found: URL does not exist.' .
$error_data,
$this->response->statusCode
);
case 405:
throw new Exception(
'Method Not Allowed: incorrect HTTP method ' . $this->request->getMethod() . ' provided.',
'Method Not Allowed: incorrect HTTP method ' . $this->request->getMethod() . ' provided.' .
$error_data,
$this->response->statusCode
);
case 410:
throw new Exception(
'Gone: resource ' . $this->request->getFullUrl() . ' has moved.',
'Gone: URL has moved.' .
$error_data,
$this->response->statusCode
);
case 415:
throw new Exception(
'Unsupported Media Type (incorrect HTTP method ' . $this->request->getMethod() . ' provided).',
'Unsupported Media Type (incorrect HTTP method ' . $this->request->getMethod() . ' provided).' .
$error_data,
$this->response->statusCode
);
case 429:
throw new Exception(
'Too many requests: client is rate limited.',
'Too many requests: client is rate limited.' .
$error_data,
$this->response->statusCode
);
case 500:
Expand All @@ -523,13 +552,14 @@ protected function executeRequest() {
'' ),
$this->response->statusCode );
case 501:
throw new Exception( 'Not Implemented: ' . $this->request->getFullUrl() . '.',
throw new Exception( 'Not Implemented.' . $error_data,
$this->response->statusCode );
case 502:
throw new Exception( 'Bad Gateway: server has an issue.',
throw new Exception( 'Bad Gateway: server has an issue.' . $error_data,
$this->response->statusCode );
default:
throw new Exception( 'Status code ' . $this->response->statusCode . ' returned for URL ' . $this->request->getFullUrl(),
throw new Exception( 'Status code ' . $this->response->statusCode . ' returned.' .
$error_data,
$this->response->statusCode );
}
}
Expand Down

0 comments on commit 339d716

Please sign in to comment.