Creates client exception from response

@param  ResponseInterface $response
@param  array $data Parsed response data

@return IdentityProviderException
514464
public static function clientException(ResponseInterface $response, $data)
    {
        return static::fromResponse(
            $response,
            isset($data['message']) ? $data['message'] : json_encode($data)
        );
    }
593041
protected function handleResponse(ResponseInterface $response)
    {
        $responseWrapper = new Response($response);
        $statusCode = (int) floor($response->getStatusCode() / 100);

        if ($statusCode === 4) {
            throw new ClientException($responseWrapper);
        } elseif ($statusCode === 5) {
            throw new ServerException($responseWrapper);
        }

        return $responseWrapper;
    }
11640
public function method($methodName, array $data = array())
    {
        $curl = new CURL();
        $url = $this->url . $methodName . '.json';
        $result = $curl->getURL($url, $data);
        $json = json_decode($result);
        if (!$result) {
            throw new Exception('No response retrieved');
        } elseif (!$json) {
            throw new Exception('Cannot parse JSON');
        } elseif ($json->error) {
            throw new Exception($result);
        }
        return $json;
    }
130303
protected function createLaravelResponse(ResponseInterface $response)
    {
        return new Response(
            (string) $response->getBody(),
            $response->getStatusCode(),
            $response->getHeaders()
        );
    }
664624
public function checkError(ResponseInterface $response)
    {
        if ($response->getStatusCode() < 400) {
            return $response;
        }

        //check if any limit was hit
        $this->checkUserRateLimit($response);
        $this->checkClientRateLimit($response);
        $this->checkPostRateLimit($response);

        $body = (string) $response->getBody();
        $responseData = json_decode($body, true);
        if (JSON_ERROR_NONE !== json_last_error()) {
            $responseData = $body;
        }

        if (\is_array($responseData) && isset($responseData['data']) && isset($responseData['data']['error'])) {
            $errorMessage = $responseData['data']['error'];
            // when uploading too fast, error is an array
            if (\is_array($responseData['data']['error'])) {
                $errorMessage = '';

                if (isset($responseData['data']['error']['message'])) {
                    $errorMessage = $responseData['data']['error']['message'];
                } elseif (isset($responseData['data']['error']['code'])) {
                    $errorMessage = 'Error code: ' . $responseData['data']['error']['code'];
                }
            }

            $message = 'Request failed with: "' . $errorMessage . '"';
            if (isset($responseData['data']['request'])) {
                $message = 'Request to: ' . $responseData['data']['request'] . ' failed with: "' . $errorMessage . '"';
            }

            throw new ErrorException($message, $response->getStatusCode());
        }

        throw new RuntimeException(\is_array($responseData) && isset($responseData['message']) ? $responseData['message'] : $responseData, $response->getStatusCode());
    }
621593
private function checkException($response)
    {
        if ((true === $this->getThrowExceptions()) and ($this->isErroneousResponse($response))) {
            throw new ClientException($response->detail, $response->status);
        }
        return $response;
    }
583612
private function decodeResponse(ResponseInterface $response)
    {
        $string = (string) $response->getBody();
        $data = json_decode($string, true);

        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new JsonDecodeException($string);
        }

        return $data;
    }
588157
protected function handleResponse( ResponseInterface $response ){

        $body = json_decode($response->getBody(), true);

        // The expected response should always be JSON, thus now an array.
        if( !is_array($body) ){
            throw new Exception\ApiException( 'Malformed JSON response from server', $response->getStatusCode(), $body, $response );
        }

        return $body;

    }
40844
protected function checkResponse(ResponseInterface $response, $data)
    {
        if ($response->getStatusCode() >= 400) {
            $errorMessage = '';
            if (!empty($data['errors'])) {
                foreach ($data['errors'] as $error) {
                    if (!empty($errorMessage)) {
                        $errorMessage .= ' , ';
                    }
                    $errorMessage .= implode(' - ', $error);
                }
            } else {
                $errorMessage = $response->getReasonPhrase();
            }
            throw new IdentityProviderException(
                $errorMessage,
                $response->getStatusCode(),
                $response
            );
        }
    }
856760
private function _createClient(array $response)
    {
        $model = new Client();
        $model->setId($response['id']);
        $model->setEmail($response['email']);
        $model->setDescription($response['description']);
        $model->setCreatedAt($response['created_at']);
        $model->setUpdatedAt($response['updated_at']);
        $model->setSubscription($this->_handleRecursive($response['subscription'], 'subscription'));
        $model->setAppId($response['app_id']);
        $model->setPayment($this->_handleRecursive($response['payment'], 'payment'));
        return $model;
    }
401174
private function createUserByOAuthUserResponse(UserResponseInterface $response): WebUserInterface
    {
        /** @var WebUserInterface $user */
        $user = $this->userFactory->createNew();

        if (!$canonicalEmail = $this->canonicalizer->canonicalize($response->getEmail())) {
            throw new AuthorizationException('phpmob.ui.oauth_response_not_found_email');
        }

        $user->setEmailCanonical($canonicalEmail);

        if ($firstName = $response->getFirstName()) {
            $user->setFirstName($firstName);
        }

        if ($lastName = $response->getLastName()) {
            $user->setLastName($lastName);
        }

        if ($displayName = $response->getRealName()) {
            $user->setDisplayName($displayName);
        }

        // set random password to prevent issue with not nullable field & potential security hole
        $user->setPlainPassword(substr(sha1($response->getAccessToken()), 0, 10));
        $user->setEnabled(true);

        return $this->updateUserByOAuthUserResponse($user, $response);
    }
28334
protected function parseTokenResponse(ResponseInterface $response):AccessToken{
		$data = Psr7\get_json($response, true);

		if(!$data || !is_array($data)){
			throw new ProviderException('unable to parse token response');
		}
		elseif(isset($data['error'])){
			throw new ProviderException('error retrieving access token: '.$data['message']);
		}
		elseif(!isset($data['session']['key'])){
			throw new ProviderException('token missing');
		}

		$token = new AccessToken([
			'provider'    => $this->serviceName,
			'accessToken' => $data['session']['key'],
			'expires'     => AccessToken::EOL_NEVER_EXPIRES,
		]);

		unset($data['session']['key']);

		$token->extraParams = $data;

		$this->storage->storeAccessToken($this->serviceName, $token);

		return $token;
	}
827649
protected function checkResponse(ResponseInterface $response, $data)
    {
        // Standard error response format
        if (! empty($data['error'])) {
            throw new IdentityProviderException(
                $response->getReasonPhrase(),
                $response->getStatusCode(),
                (string) $response->getBody()
            );
        }
    }
890144
public static function create(Response $response)
    {
        $errorMessage = $response->getError();
        $exception = isset(static::$exceptionMap[$errorMessage])
            ? static::$exceptionMap[$errorMessage]
            : CentrifugoException::class;

        return new static($response, new $exception($errorMessage));
    }
1182949
protected function checkResponse(ResponseInterface $response, $data)
    {
        $acceptableStatuses = [200, 201];

        if (!in_array($response->getStatusCode(), $acceptableStatuses)) {
            throw new IdentityProviderException(
                $data['message'] ?: $response->getReasonPhrase(),
                $response->getStatusCode(),
                $response
            );
        }
    }
202565
private static function createException(RequestInterface $request,
        ResponseInterface $altered,
        \Exception $previous = null,
        array $ctx = []
    ) {
        if ($altered->getStatusCode() >= 400 && $altered->getStatusCode() < 500) {
            return new ClientException($request, $altered, $previous, $ctx);
        }

        return new ServerException($request, $altered, $previous, $ctx);
    }
470253
protected static function throwTransportableExceptionIfNeeded(
        array $response,
        string $url = ''
    ) {
        if (
            !isset($response['code']) ||
            0 === $response['code']
        ) {
            throw ConnectionException::buildConnectExceptionByUrl($url);
        }

        switch ($response['code']) {
            case ResourceNotAvailableException::getTransportableHTTPError():
                throw new ResourceNotAvailableException($response['body']['message']);
            case InvalidTokenException::getTransportableHTTPError():
                throw new InvalidTokenException($response['body']['message']);
            case InvalidFormatException::getTransportableHTTPError():
                throw new InvalidFormatException($response['body']['message']);
            case ResourceExistsException::getTransportableHTTPError():
                throw new ResourceExistsException($response['body']['message']);
            case ForbiddenException::getTransportableHTTPError():
                throw new ForbiddenException($response['body']['message']);
            case ConnectionException::getTransportableHTTPError():
                throw new ConnectionException('Apisearch returned an internal error code [500]');
        }
    }