From 235b020ad48cf9c0d2cdcb067b34d1424f0571f6 Mon Sep 17 00:00:00 2001 From: Furkan Yilmaz Date: Fri, 29 Jul 2016 12:53:01 +0100 Subject: [PATCH 1/6] Changed error message from always "Bad Request" to api.cloudflare error message --- src/API/AbstractAPIClient.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/API/AbstractAPIClient.php b/src/API/AbstractAPIClient.php index 39acbf2..485be4c 100644 --- a/src/API/AbstractAPIClient.php +++ b/src/API/AbstractAPIClient.php @@ -65,6 +65,12 @@ public function callAPI(Request $request) return $response; } catch (RequestException $e) { + $jsonResponse = json_decode($e->getResponse()->getBody(), true); + $errorMessage = $e->getMessage(); + if (count($jsonResponse['errors']) > 0) { + $errorMessage = $jsonResponse['errors'][0]['message']; + } + $this->logAPICall($this->getAPIClientName(), array( 'type' => 'request', 'method' => $request->getMethod(), @@ -72,9 +78,9 @@ public function callAPI(Request $request) 'headers' => $request->getHeaders(), 'params' => $request->getParameters(), 'body' => $request->getBody(), ), true); - $this->logAPICall($this->getAPIClientName(), array('type' => 'response', 'code' => $e->getCode(), 'body' => $e->getMessage(), 'stacktrace' => $e->getTraceAsString()), true); + $this->logAPICall($this->getAPIClientName(), array('type' => 'response', 'code' => $e->getCode(), 'body' => $errorMessage, 'stacktrace' => $e->getTraceAsString()), true); - return $this->createAPIError($e->getMessage()); + return $this->createAPIError($errorMessage); } } From 26c24e82cb159389812e62b69ca7c84384f89e80 Mon Sep 17 00:00:00 2001 From: Furkan Yilmaz Date: Fri, 29 Jul 2016 14:25:38 +0100 Subject: [PATCH 2/6] Added getErrorMessage for APIClient's to be able to extend the error message --- src/API/AbstractAPIClient.php | 19 +++++++++++++++++-- src/API/Client.php | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/API/AbstractAPIClient.php b/src/API/AbstractAPIClient.php index 485be4c..3e39367 100644 --- a/src/API/AbstractAPIClient.php +++ b/src/API/AbstractAPIClient.php @@ -84,7 +84,22 @@ public function callAPI(Request $request) } } - public function logAPICall($api, $message, $isError) + /** + * @param RequestException $object + * + * @return string + */ + public function getErrorMessage(RequestException $error) + { + return $error->getMessage(); + } + + /** + * @param string $apiName + * @param array $message + * @param bool $isError + */ + public function logAPICall(string $apiName, array $message, bool $isError) { $logLevel = 'error'; if ($isError === false) { @@ -95,7 +110,7 @@ public function logAPICall($api, $message, $isError) $message = print_r($message, true); } - $this->logger->$logLevel('['.$api.'] '.$message); + $this->logger->$logLevel('['.$apiName.'] '.$message); } /** diff --git a/src/API/Client.php b/src/API/Client.php index 607032d..aba3056 100644 --- a/src/API/Client.php +++ b/src/API/Client.php @@ -48,6 +48,22 @@ public function createAPIError($message) ); } + /** + * @param RequestException object + * + * @return string + */ + public function getErrorMessage($error) + { + $jsonResponse = json_decode($error->getResponse()->getBody(), true); + $errorMessage = $error->getMessage(); + if (count($jsonResponse['errors']) > 0) { + $errorMessage = $jsonResponse['errors'][0]['message']; + } + + return $errorMessage; + } + /** * @param $response * From 220ce2234b59e7e829eba3dcf8a37521e41a8ab3 Mon Sep 17 00:00:00 2001 From: Furkan Yilmaz Date: Fri, 29 Jul 2016 14:29:20 +0100 Subject: [PATCH 3/6] added missing getErrorMessage call --- src/API/AbstractAPIClient.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/API/AbstractAPIClient.php b/src/API/AbstractAPIClient.php index 3e39367..cbf8db4 100644 --- a/src/API/AbstractAPIClient.php +++ b/src/API/AbstractAPIClient.php @@ -65,11 +65,7 @@ public function callAPI(Request $request) return $response; } catch (RequestException $e) { - $jsonResponse = json_decode($e->getResponse()->getBody(), true); - $errorMessage = $e->getMessage(); - if (count($jsonResponse['errors']) > 0) { - $errorMessage = $jsonResponse['errors'][0]['message']; - } + $errorMessage = getErrorMessage($e); $this->logAPICall($this->getAPIClientName(), array( 'type' => 'request', From 948ca1d609d51b5210d8d56eea9183c666c5008a Mon Sep 17 00:00:00 2001 From: Furkan Yilmaz Date: Fri, 29 Jul 2016 17:07:53 +0100 Subject: [PATCH 4/6] added abstract function createAPIError --- src/API/AbstractAPIClient.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/API/AbstractAPIClient.php b/src/API/AbstractAPIClient.php index cbf8db4..d095bc7 100644 --- a/src/API/AbstractAPIClient.php +++ b/src/API/AbstractAPIClient.php @@ -120,4 +120,11 @@ abstract public function beforeSend(Request $request); * @return mixed */ abstract public function getAPIClientName(); + + /** + * @param $message + * + * @return array + */ + abstract public function createAPIError($message); } From cd704ee1946c91ff8b9c1ad9e0a8950a6b4031b5 Mon Sep 17 00:00:00 2001 From: Furkan Yilmaz Date: Fri, 29 Jul 2016 17:08:59 +0100 Subject: [PATCH 5/6] added type check for Client\getErrorMessage --- src/API/Client.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/API/Client.php b/src/API/Client.php index aba3056..2405c9e 100644 --- a/src/API/Client.php +++ b/src/API/Client.php @@ -2,6 +2,8 @@ namespace CF\API; +use GuzzleHttp\Exception\RequestException; + class Client extends AbstractAPIClient { const CLIENT_API_NAME = 'CLIENT API'; @@ -49,14 +51,15 @@ public function createAPIError($message) } /** - * @param RequestException object + * @param RequestException error * * @return string */ - public function getErrorMessage($error) + public function getErrorMessage(RequestException $error) { $jsonResponse = json_decode($error->getResponse()->getBody(), true); $errorMessage = $error->getMessage(); + if (count($jsonResponse['errors']) > 0) { $errorMessage = $jsonResponse['errors'][0]['message']; } From a7775b1aa95aa49dedc250ba141a18ca4a0513bd Mon Sep 17 00:00:00 2001 From: Furkan Yilmaz Date: Fri, 29 Jul 2016 17:09:15 +0100 Subject: [PATCH 6/6] added test --- src/Test/API/ClientTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Test/API/ClientTest.php b/src/Test/API/ClientTest.php index 3451538..2bb7904 100644 --- a/src/Test/API/ClientTest.php +++ b/src/Test/API/ClientTest.php @@ -80,4 +80,37 @@ public function testResponseOkReturnsTrueForValidResponse() $this->assertTrue($this->mockClientAPI->responseOk($v4APIResponse)); } + + public function testGetErrorMessageSuccess() + { + $errorMessage = 'I am an error message'; + + $error = $this->getMockBuilder('GuzzleHttp\Exception\RequestException') + ->disableOriginalConstructor() + ->setMethods(array('getResponse', 'getBody', 'getMessage')) + ->getMock(); + + $errorJSON = json_encode( + array( + 'success' => false, + 'errors' => array( + array( + 'message' => $errorMessage, + ), + ), + ) + ); + + $error->expects($this->any()) + ->method('getMessage') + ->will($this->returnValue('Not this message')); + $error->expects($this->any()) + ->method('getResponse') + ->will($this->returnSelf()); + $error->expects($this->any()) + ->method('getBody') + ->will($this->returnValue($errorJSON)); + + $this->assertEquals($errorMessage, $this->mockClientAPI->getErrorMessage($error)); + } }