Skip to content

Commit

Permalink
Add improved exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmaguire committed Apr 14, 2016
1 parent 2fc2a60 commit 0c8b940
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
53 changes: 53 additions & 0 deletions src/Provider/Exception/GithubIdentityProviderException.php
@@ -0,0 +1,53 @@
<?php

namespace League\OAuth2\Client\Provider\Exception;

use Psr\Http\Message\ResponseInterface;

class GithubIdentityProviderException extends IdentityProviderException
{
/**
* Creates client exception from response.
*
* @param ResponseInterface $response
* @param string $data Parsed response data
*
* @return IdentityProviderException
*/
public static function clientException(ResponseInterface $response, $data)
{
return static::fromResponse(
$response,
isset($data['message']) ? $data['message'] : $response->getReasonPhrase()
);
}

/**
* Creates oauth exception from response.
*
* @param ResponseInterface $response
* @param string $data Parsed response data
*
* @return IdentityProviderException
*/
public static function oauthException(ResponseInterface $response, $data)
{
return static::fromResponse(
$response,
isset($data['error']) ? $data['error'] : $response->getReasonPhrase()
);
}

/**
* Creates identity exception from response.
*
* @param ResponseInterface $response
* @param string $message
*
* @return IdentityProviderException
*/
protected static function fromResponse(ResponseInterface $response, $message = null)
{
return new static($message, $response->getStatusCode(), (string) $response->getBody());
}
}
11 changes: 5 additions & 6 deletions src/Provider/Github.php
Expand Up @@ -2,7 +2,7 @@

namespace League\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Provider\Exception\GithubIdentityProviderException;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -79,6 +79,7 @@ protected function getDefaultScopes()
* Check a provider response for errors.
*
* @link https://developer.github.com/v3/#client-errors
* @link https://developer.github.com/v3/oauth/#common-errors-for-the-access-token-request
* @throws IdentityProviderException
* @param ResponseInterface $response
* @param string $data Parsed response data
Expand All @@ -87,11 +88,9 @@ protected function getDefaultScopes()
protected function checkResponse(ResponseInterface $response, $data)
{
if ($response->getStatusCode() >= 400) {
$errorMessage = isset($data['message']) ? $data['message'] : $response->getReasonPhrase();
throw new IdentityProviderException($errorMessage, $response->getStatusCode(), $response);
} elseif (isset($data['error'], $data['error_description'])) {
$errorMessage = $data['error'] . ': ' . $data['error_description'];
throw new IdentityProviderException($errorMessage, $response->getStatusCode(), $response);
throw GithubIdentityProviderException::clientException($response, $data);
} elseif (isset($data['error'])) {
throw GithubIdentityProviderException::oauthException($response, $data);
}
}

Expand Down

0 comments on commit 0c8b940

Please sign in to comment.