Skip to content

Commit

Permalink
Merge pull request #271 from shadowhand/feature/provider-error-responses
Browse files Browse the repository at this point in the history
Add new getResponseErrorMessage method
  • Loading branch information
ramsey committed Apr 25, 2015
2 parents 9f2725a + 059cdda commit ea977f9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/Exception/IDPException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class IDPException extends \Exception

public function __construct($result)
{
if (!empty($result['error']) && is_array($result['error'])) {
// Error response is wrapped in a top entity type, JSON:API style.
$result = $result['error'];
}

$this->result = $result;

$code = isset($result['code']) ? $result['code'] : 0;
Expand Down
45 changes: 31 additions & 14 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,36 @@ public function getAccessToken($grant = 'authorization_code', $params = [])
// @codeCoverageIgnoreEnd
}

$result = $this->prepareResponse($response);

if (isset($result['error']) && ! empty($result['error'])) {
// @codeCoverageIgnoreStart
throw new IDPException($result);
// @codeCoverageIgnoreEnd
}

$result = $this->prepareAccessTokenResult($result);

return $grant->handleResponse($result);
}

/**
* Prepare the response, parsing according to configuration and returning
* the response as an array.
*
* @param string $response
* @return array
*/
protected function prepareResponse($response)
{
$result = [];

switch ($this->responseType) {
case 'json':
$result = json_decode($response, true);
$json = json_decode($response, true);

if (JSON_ERROR_NONE !== json_last_error()) {
$result = [];
if (JSON_ERROR_NONE === json_last_error()) {
$result = $json;
}

break;
Expand All @@ -216,15 +240,7 @@ public function getAccessToken($grant = 'authorization_code', $params = [])
break;
}

if (isset($result['error']) && ! empty($result['error'])) {
// @codeCoverageIgnoreStart
throw new IDPException($result);
// @codeCoverageIgnoreEnd
}

$result = $this->prepareAccessTokenResult($result);

return $grant->handleResponse($result);
return $result;
}

/**
Expand Down Expand Up @@ -349,8 +365,9 @@ protected function fetchProviderData($url, array $headers = [])
$response = $request->getBody();
} catch (BadResponseException $e) {
// @codeCoverageIgnoreStart
$raw_response = explode("\n", $e->getResponse());
throw new IDPException(end($raw_response));
$response = $e->getResponse()->getBody();
$result = $this->prepareResponse($response);
throw new IDPException($result);
// @codeCoverageIgnoreEnd
}

Expand Down
7 changes: 7 additions & 0 deletions test/src/Exception/IDPExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function testAsStringWithCode()
$this->assertEquals('message: 404: message', (string)$exception);
}

public function testAsWrapped()
{
$exception = new IDPException(array('error' => array('error' => 'message')));

$this->assertEquals('message: message', (string)$exception);
}

public function testGetResponseBody()
{
$exception = new IDPException(array('error' => 'message', 'code' => 404));
Expand Down

0 comments on commit ea977f9

Please sign in to comment.