From 308974a60405e6fe2f0bb24e39319f420954030f Mon Sep 17 00:00:00 2001 From: simialbi Date: Thu, 25 Apr 2019 12:51:48 +0200 Subject: [PATCH] added rest exception and `enableExceptions` parameter fixed tests --- src/Connection.php | 25 ++++++++++++++++++------- src/Exception.php | 19 +++++++++++++++++++ tests/RelationTest.php | 2 +- tests/UrlTest.php | 6 +++--- tests/UrlWithoutPluralisationTest.php | 6 +++--- 5 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 src/Exception.php diff --git a/src/Connection.php b/src/Connection.php index 48dbbb7..b9b1660 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -60,6 +60,10 @@ class Connection extends Component * @var boolean Whether to user pluralisation or not */ public $usePluralisation = true; + /** + * @var boolean Whether the connection should throw an exception if response is not 200 or not + */ + public $enableExceptions = false; /** * @var boolean Whether we are in test mode or not (prevent execution) */ @@ -172,7 +176,7 @@ public function getQueryBuilder() * @param array $data request body * * @return mixed response - * @throws \yii\httpclient\Exception + * @throws Exception */ public function get($url, $data = []) { @@ -187,7 +191,7 @@ public function get($url, $data = []) * @param array $data request body * * @return HeaderCollection response - * @throws \yii\httpclient\Exception + * @throws Exception */ public function head($url, $data = []) { @@ -204,7 +208,7 @@ public function head($url, $data = []) * @param array $data request body * * @return mixed response - * @throws \yii\httpclient\Exception + * @throws Exception */ public function post($url, $data = []) { @@ -218,7 +222,7 @@ public function post($url, $data = []) * @param array $data request body * * @return mixed response - * @throws \yii\httpclient\Exception + * @throws Exception */ public function put($url, $data = []) { @@ -232,7 +236,7 @@ public function put($url, $data = []) * @param array $data request body * * @return mixed response - * @throws \yii\httpclient\Exception + * @throws Exception */ public function delete($url, $data = []) { @@ -271,7 +275,7 @@ public function getHandler() * @param array $data the request data * * @return mixed|false - * @throws \yii\httpclient\Exception + * @throws Exception */ protected function request($method, $url, $data = []) { @@ -303,10 +307,17 @@ protected function request($method, $url, $data = []) Yii::debug($headers, __METHOD__ . '-headers'); $request = call_user_func([$this->handler, $method], $url, $data, $headers); - $this->_response = $this->isTestMode ? [] : $request->send(); + try { + $this->_response = $this->isTestMode ? [] : $request->send(); + } catch (\yii\httpclient\Exception $e) { + throw new Exception('Request failed', [], 0, $e); + } Yii::endProfile($profile, __METHOD__); if (!$this->isTestMode && !$this->_response->isOk) { + if ($this->enableExceptions) { + throw new Exception($this->_response->content, $this->_response->headers->toArray()); + } return false; } diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 0000000..e1a3b7c --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,19 @@ + + * @copyright Copyright © 2019 Simon Karlen + */ + +namespace simialbi\yii2\rest; + +class Exception extends \yii\db\Exception +{ + /** + * {@inheritDoc} + */ + public function getName() + { + return 'REST Exception'; + } +} diff --git a/tests/RelationTest.php b/tests/RelationTest.php index 7067be9..602a01e 100644 --- a/tests/RelationTest.php +++ b/tests/RelationTest.php @@ -40,6 +40,6 @@ public function testRelationGet() $logEntry = $this->parseLogs(); $this->assertEquals('GET', $logEntry['method']); - $this->assertEquals('https://api.site.com/related-rest-models?filter%5Brest_model_id%5D=1', $logEntry['url']); + $this->assertStringStartsWith('https://api.site.com/related-rest-models?filter%5Brest_model_id%5D=1', $logEntry['url']); } } diff --git a/tests/UrlTest.php b/tests/UrlTest.php index b30a08e..851d797 100644 --- a/tests/UrlTest.php +++ b/tests/UrlTest.php @@ -27,7 +27,7 @@ public function testGetOne() $logEntry = $this->parseLogs(); $this->assertEquals('GET', $logEntry['method']); - $this->assertEquals('https://api.site.com/rest-models/1', $logEntry['url']); + $this->assertStringStartsWith('https://api.site.com/rest-models/1', $logEntry['url']); } public function testGetAnotherOne() @@ -37,7 +37,7 @@ public function testGetAnotherOne() $logEntry = $this->parseLogs(); $this->assertEquals('GET', $logEntry['method']); - $this->assertEquals('https://api.site.com/rest-models/1', $logEntry['url']); + $this->assertStringStartsWith('https://api.site.com/rest-models/1', $logEntry['url']); } public function testDeleteOne() @@ -68,6 +68,6 @@ public function testDeleteOne() $logEntry = $this->parseLogs(); $this->assertEquals('DELETE', $logEntry['method']); - $this->assertEquals('https://api.site.com/rest-models/1', $logEntry['url']); + $this->assertStringStartsWith('https://api.site.com/rest-models/1', $logEntry['url']); } } diff --git a/tests/UrlWithoutPluralisationTest.php b/tests/UrlWithoutPluralisationTest.php index 12673cb..357db06 100644 --- a/tests/UrlWithoutPluralisationTest.php +++ b/tests/UrlWithoutPluralisationTest.php @@ -34,7 +34,7 @@ public function testGetOne() $logEntry = $this->parseLogs(); $this->assertEquals('GET', $logEntry['method']); - $this->assertEquals('https://api.site.com/rest-model/1', $logEntry['url']); + $this->assertStringStartsWith('https://api.site.com/rest-model/1', $logEntry['url']); } public function testGetAnotherOne() @@ -44,7 +44,7 @@ public function testGetAnotherOne() $logEntry = $this->parseLogs(); $this->assertEquals('GET', $logEntry['method']); - $this->assertEquals('https://api.site.com/rest-model/1', $logEntry['url']); + $this->assertStringStartsWith('https://api.site.com/rest-model/1', $logEntry['url']); } public function testDeleteOne() @@ -75,6 +75,6 @@ public function testDeleteOne() $logEntry = $this->parseLogs(); $this->assertEquals('DELETE', $logEntry['method']); - $this->assertEquals('https://api.site.com/rest-model/1', $logEntry['url']); + $this->assertStringStartsWith('https://api.site.com/rest-model/1', $logEntry['url']); } }