#825 Introduce ResponseParsingException and throw this exception in …

Open tandhika

@@ -0,0 +1,79 @@
Loading
1 +
<?php
2 +
/**
3 +
 * This file is part of the league/oauth2-client library
4 +
 *
5 +
 * For the full copyright and license information, please view the LICENSE
6 +
 * file that was distributed with this source code.
7 +
 *
8 +
 * @copyright Copyright (c) Alex Bilbie <hello@alexbilbie.com>
9 +
 * @license http://opensource.org/licenses/MIT MIT
10 +
 * @link http://thephpleague.com/oauth2-client/ Documentation
11 +
 * @link https://packagist.org/packages/league/oauth2-client Packagist
12 +
 * @link https://github.com/thephpleague/oauth2-client GitHub
13 +
 */
14 +
15 +
namespace League\OAuth2\Client\Provider\Exception;
16 +
17 +
use Exception;
18 +
use Psr\Http\Message\ResponseInterface;
19 +
use UnexpectedValueException;
20 +
21 +
/**
22 +
 * Exception thrown if the parser cannot parse the provider response.
23 +
 */
24 +
class ResponseParsingException extends UnexpectedValueException
25 +
{
26 +
    /**
27 +
     * @var ResponseInterface
28 +
     */
29 +
    protected $response;
30 +
31 +
    /**
32 +
     * @var string
33 +
     */
34 +
    protected $responseBody;
35 +
36 +
    /**
37 +
     * @param ResponseInterface $response The response
38 +
     * @param string $responseBody The response body
39 +
     * @param null $message
40 +
     * @param int $code
41 +
     * @param Exception|null $previous
42 +
     */
43 +
    public function __construct(
44 +
        ResponseInterface $response,
45 +
        $responseBody,
46 +
        $message = null,
47 +
        $code = 0,
48 +
        Exception $previous = null
49 +
    ) {
50 +
        $this->response = $response;
51 +
        $this->responseBody = $responseBody;
52 +
53 +
        if (null === $message) {
54 +
            $message = sprintf('Cannot parse response body: "%s"', $responseBody);
55 +
        }
56 +
57 +
        parent::__construct($message, $code, $previous);
58 +
    }
59 +
60 +
    /**
61 +
     * Returns the exception's response.
62 +
     *
63 +
     * @return ResponseInterface
64 +
     */
65 +
    public function getResponse()
66 +
    {
67 +
        return $this->response;
68 +
    }
69 +
70 +
    /**
71 +
     * Returns the exception's response body.
72 +
     *
73 +
     * @return string
74 +
     */
75 +
    public function getResponseBody()
76 +
    {
77 +
        return $this->responseBody;
78 +
    }
79 +
}

@@ -22,6 +22,7 @@
Loading
22 22
use League\OAuth2\Client\OptionProvider\OptionProviderInterface;
23 23
use League\OAuth2\Client\OptionProvider\PostAuthOptionProvider;
24 24
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
25 +
use League\OAuth2\Client\Provider\Exception\ResponseParsingException;
25 26
use League\OAuth2\Client\Token\AccessToken;
26 27
use League\OAuth2\Client\Token\AccessTokenInterface;
27 28
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
@@ -520,6 +521,8 @@
Loading
520 521
     * @param  mixed $grant
521 522
     * @param  array $options
522 523
     * @throws IdentityProviderException
524 +
     * @throws ResponseParsingException if the flag $mayThrowResponseParsingException is true and
525 +
     *                                  response body cannot be parsed.
523 526
     * @return AccessTokenInterface
524 527
     */
525 528
    public function getAccessToken($grant, array $options = [])
@@ -613,6 +616,8 @@
Loading
613 616
     *
614 617
     * @param  RequestInterface $request
615 618
     * @throws IdentityProviderException
619 +
     * @throws ResponseParsingException if the flag $mayThrowResponseParsingException is true and
620 +
     *                                  response body cannot be parsed.
616 621
     * @return mixed
617 622
     */
618 623
    public function getParsedResponse(RequestInterface $request)
@@ -665,9 +670,9 @@
Loading
665 670
    /**
666 671
     * Parses the response according to its content-type header.
667 672
     *
668 -
     * @throws UnexpectedValueException
673 +
     * @throws ResponseParsingException if response body cannot be parsed.
669 674
     * @param  ResponseInterface $response
670 -
     * @return array
675 +
     * @return array|string
671 676
     */
672 677
    protected function parseResponse(ResponseInterface $response)
673 678
    {
@@ -686,11 +691,14 @@
Loading
686 691
            return $this->parseJson($content);
687 692
        } catch (UnexpectedValueException $e) {
688 693
            if (strpos($type, 'json') !== false) {
689 -
                throw $e;
694 +
                throw new ResponseParsingException($response, $content, $e->getMessage(), $e->getCode(), $e);
690 695
            }
691 696
697 +
            // for any other content types
692 698
            if ($response->getStatusCode() == 500) {
693 -
                throw new UnexpectedValueException(
699 +
                throw new ResponseParsingException(
700 +
                    $response,
701 +
                    $content,
694 702
                    'An OAuth server error was encountered that did not contain a JSON body',
695 703
                    0,
696 704
                    $e
@@ -774,6 +782,8 @@
Loading
774 782
     *
775 783
     * @param  AccessToken $token
776 784
     * @return mixed
785 +
     * @throws ResponseParsingException if the flag $mayThrowResponseParsingException is true and
786 +
     *                                  response body cannot be parsed.
777 787
     */
778 788
    protected function fetchResourceOwnerDetails(AccessToken $token)
779 789
    {

Learn more Showing 1 files with coverage changes found.

New file src/Provider/Exception/ResponseParsingException.php
New
Loading file...
Files Complexity Coverage
src ø 100.00%
Project Totals (21 files) 184 100.00%
Loading