Creates client exception from response.

@param  ResponseInterface $response
@param  string $data Parsed response data
@return IdentityProviderException
187609
public static function clientException(ResponseInterface $response, $data)
    {
        return new static($response->getReasonPhrase(), $response->getStatusCode(), (string) $response->getBody());
    }
731932
private function defaultErrorHandler(
        ServerRequestInterface $request,
        ResponseInterface $response
    ) {
        $response = $response->withStatus(422, 'Unprocessable Entity')
            ->withHeader('Content-Type', 'application/json');

        $middlewareAttribute = $request->getAttribute(Middleware::KEY, []);

        if (isset($middlewareAttribute[self::KEY])) {
            /** @var ResponseInterface $response */
            $stream = $response->getBody();
            $stream->write(json_encode($middlewareAttribute[self::KEY], JSON_UNESCAPED_SLASHES));
        }

        return $response;
    }
648520
private function withError(ResponseInterface $response, $throwable)
    {
        $text = 'Application Error';

        if ($this->displayErrorDetails) {
            $throwables = $this->unpackThrowables($throwable);
            $text = [
                'Application Error',
                $throwable->getMessage(),
                '',
                'Error Details:',
                $this->formatStacktraceForExceptions($throwables),
            ];
        }

        return $this
            ->withText($response, $text)
            ->withStatus(500);
    }
245063
public static function factory(RequestInterface $request, Response $response)
    {
        $message = 'Client error response' . PHP_EOL . implode(PHP_EOL, array(
            '[status code] ' . $response->getStatusCode(),
            '[reason phrase] ' . $response->getReasonPhrase(),
            '[url] ' . $request->getUrl(),
        ));

        $e = new static($message);
        $e->setResponse($response);
        $e->setRequest($request);

        return $e;
    }
785392
protected function exception(ConnectionInterface $connection, ErrorResponseInterface $response)
    {
        $connection->disconnect();
        $message = $response->getMessage();

        throw new ServerException($message);
    }
216256
public static function createFromResponse($responseData)
    {
        $messageStatus = new self(null);
        $messageStatus->setRawResponseData($responseData)
            ->processResponse();

        if ($messageStatus['status'] !== 0 && $messageStatus['status'] !== '0') {
            throw new Exception($messageStatus['err_msg']);
        }

        //show message status instead of integer status code
        $messageStatus['message_status'] = self::$messageStatusType[$messageStatus['message_status']];

        return $messageStatus;
    }
1319142
public static function create(
      RequestInterface $request,
      ResponseInterface $response = null,
      \Exception $previous = null,
      array $ctx = []
    ) {
        $e = parent::create($request, $response, $previous);
        if ($response === null) {
            return $e;
        }

        // Re-create the exception to alter the message.
        $details = self::getErrorDetails($response);
        if (!empty($details)) {
            $className = get_class($e);
            $e = new $className($e->getMessage() . $details, $e->getRequest(), $e->getResponse());
        }

        return $e;
    }
751721
public function execute(ResponseInterface $response): ResponseInterface
    {
        if ($response->getStatusCode() !== self::STATUS_CODE_OK) {
            throw ExceptionFactory::createExceptionForResponse(
                $this->fetchErrorMessages($response),
                $response->getStatusCode(),
                $this->getResponseId($response)
            );
        }

        return $response;
    }
1215640
protected function decodeBody(Response $response)
    {
        if ($response->isServerError()) {
            throw new Exception\ClientException(sprintf(
                "'%s' provider encountered a '%s' error while querying '%s'",
                $this->getIdentifier(),
                $response->getReasonPhrase(),
                $this->uri
            ));
        }

        $body = Json::decode($response->getBody());

        if ($response->isClientError()) {
            throw new Exception\ClientException($body->meta->error_message, $response->getStatusCode());
        }

        return $body->data;
    }
880902
private function createCustomException(ClientException $exception): Exception
    {
        $errorId = '';
        $response = $exception->getResponse();
        if ($response) {
            $data = guzzle_json_decode((string) $response->getBody(), true);
            $errorId = (string) $data['sys']['id'] ?? '';
        }

        $exceptionClass = $this->getExceptionClass($errorId);

        return new $exceptionClass($exception);
    }
125590
protected function checkResponseErrors(ResponseInterface $response)
	{
		$status = intval($response->getStatusCode());

		if ($status === 200) return;

		switch ($status) {
			case 401:
				throw new AuthorizationException($response);
			default:
				throw new RequestException($response);
		}
	}
1019653
protected function errorCheckResponse(ResponseInterface $response)
    {
        $body = $response->getBody();

        if ($response->getStatusCode() >= 400) {
            throw new HttpException($response);
        }

        $responseObject = @json_decode($this->streamToString($body));
        if (!isset($responseObject)) {
            throw new Exception('JSON returned is not valid', $response);
        }

        if (isset($responseObject->error)) {
            throw new Exception($responseObject->error, $response);
        }
    }
996545
public static function clientException(ResponseInterface $response, $data)
    {
        return static::fromResponse(
            $response,
            isset($data['message']) ? $data['message'] : $response->getReasonPhrase()
        );
    }
276985
public static function createFromResponse($responseData)
    {
        $verifyCode = new self(null, null);
        $verifyCode->setRawResponseData($responseData)
            ->processResponse();

        if ($verifyCode['status'] !== 0 && $verifyCode['status'] !== '0') {
            throw new Exception($verifyCode['err_msg']);
        }

        return $verifyCode;
    }
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);
    }
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;

    }
550450
public function createDbalException(Response $response, DriverExceptionInterface $exception) {
        switch ($response->getStatusCode()) {
            case Response::HTTP_BAD_REQUEST:
                return new DBALException\SyntaxErrorException($response->getContent(), $exception);

            case Response::HTTP_METHOD_NOT_ALLOWED:
            case Response::HTTP_NOT_ACCEPTABLE:
            case Response::HTTP_REQUEST_TIMEOUT:
            case Response::HTTP_LENGTH_REQUIRED:
            case Response::HTTP_INTERNAL_SERVER_ERROR:
                return new DBALException\ServerException($response->getContent(), $exception);
            
            case Response::HTTP_CONFLICT:
                return new DBALException\ConstraintViolationException($response->getContent(), $exception);

            case Response::HTTP_UNAUTHORIZED:
            case Response::HTTP_FORBIDDEN:
            case Response::HTTP_PROXY_AUTHENTICATION_REQUIRED:
            case Response::HTTP_BAD_GATEWAY:
            case Response::HTTP_SERVICE_UNAVAILABLE:
            case Response::HTTP_GATEWAY_TIMEOUT:
                return new DBALException\ConnectionException($response->getContent(), $exception);

            default:
                return new DBALException\DriverException($response->getContent(), $exception);
        }
    }