Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5 from cloudflare/thellimist/PI-710
Browse files Browse the repository at this point in the history
Changed error message from always "Bad Request" to api.cloudflare err…
  • Loading branch information
jwineman authored Jul 29, 2016
2 parents ea9d01e + a7775b1 commit 4f798ef
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/API/AbstractAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,37 @@ public function callAPI(Request $request)

return $response;
} catch (RequestException $e) {
$errorMessage = getErrorMessage($e);

$this->logAPICall($this->getAPIClientName(), array(
'type' => 'request',
'method' => $request->getMethod(),
'path' => $request->getUrl(),
'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);
}
}

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) {
Expand All @@ -89,7 +106,7 @@ public function logAPICall($api, $message, $isError)
$message = print_r($message, true);
}

$this->logger->$logLevel('['.$api.'] '.$message);
$this->logger->$logLevel('['.$apiName.'] '.$message);
}

/**
Expand All @@ -103,4 +120,11 @@ abstract public function beforeSend(Request $request);
* @return mixed
*/
abstract public function getAPIClientName();

/**
* @param $message
*
* @return array
*/
abstract public function createAPIError($message);
}
19 changes: 19 additions & 0 deletions src/API/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CF\API;

use GuzzleHttp\Exception\RequestException;

class Client extends AbstractAPIClient
{
const CLIENT_API_NAME = 'CLIENT API';
Expand Down Expand Up @@ -48,6 +50,23 @@ public function createAPIError($message)
);
}

/**
* @param RequestException error
*
* @return string
*/
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'];
}

return $errorMessage;
}

/**
* @param $response
*
Expand Down
33 changes: 33 additions & 0 deletions src/Test/API/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 4f798ef

Please sign in to comment.