diff --git a/src/Provider/Exception/GithubIdentityProviderException.php b/src/Provider/Exception/GithubIdentityProviderException.php new file mode 100644 index 0000000..bb30ace --- /dev/null +++ b/src/Provider/Exception/GithubIdentityProviderException.php @@ -0,0 +1,53 @@ +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()); + } +} diff --git a/src/Provider/Github.php b/src/Provider/Github.php index 5addaf2..9946adf 100644 --- a/src/Provider/Github.php +++ b/src/Provider/Github.php @@ -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; @@ -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 @@ -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); } }