Skip to content

Commit 2913e20

Browse files
committed
+ Connection::errorChecker callback to test if API response was error
1 parent b1266af commit 2913e20

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/Connection.php

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace hiqdev\hiart;
1313

14-
use common\components\Err;
1514
use Yii;
1615
use yii\base\Component;
1716
use yii\base\InvalidConfigException;
@@ -41,10 +40,10 @@ class Connection extends Component
4140
private function _getAuth()
4241
{
4342
$res = [];
44-
if (\Yii::$app->user->identity) {
45-
$res['access_token'] = \Yii::$app->user->identity->getAccessToken();
43+
if (Yii::$app->user->identity) {
44+
$res['access_token'] = Yii::$app->user->identity->getAccessToken();
4645
} else {
47-
\Yii::$app->user->loginRequired();
46+
Yii::$app->user->loginRequired();
4847
}
4948

5049
return $res;
@@ -265,7 +264,7 @@ private function createUrl($path, $options = [])
265264
protected function httpRequest($method, $url, $requestBody = null, $raw = false)
266265
{
267266
$this->auth = [
268-
'access_token' => \Yii::$app->user->identity->getAccessToken(),
267+
'access_token' => Yii::$app->user->identity->getAccessToken(),
269268
];
270269
$method = strtoupper($method);
271270
// response body and headers
@@ -415,6 +414,11 @@ protected function decodeErrorBody($body)
415414
}
416415
}
417416

417+
/**
418+
* Callback to test if API response has error.
419+
*/
420+
public $errorChecker;
421+
418422
/**
419423
* @param array $response response data from API
420424
* @param string $url request URL
@@ -426,8 +430,9 @@ protected function decodeErrorBody($body)
426430
*/
427431
protected function checkResponse($response, $url, $options)
428432
{
429-
if (Err::is($response)) {
430-
throw new ErrorResponseException(Err::get($response), [
433+
$error = call_user_func($this->errorChecker, $response);
434+
if ($error) {
435+
throw new ErrorResponseException($error, [
431436
'requestUrl' => $url,
432437
'request' => $options,
433438
'response' => $response,

0 commit comments

Comments
 (0)