Handle HTTP errors.

Call is controlled by the specific API methods.

@param ResponseInterface $response

@throws DomainException
434345
protected function handleErrors(ResponseInterface $response)
    {
        $body = $response->getBody()->__toString();

        $content = json_decode($body, true);
        $message = '';
        if (JSON_ERROR_NONE === json_last_error()) {
            $message = $content['error'];
        }

        switch ($response->getStatusCode()) {
            case 401:
                throw new DomainExceptions\InvalidApiKeyException($message);
                break;
            case 403:
                throw new DomainExceptions\InsufficientPrivilegesException($message);
                break;
            case 404:
                throw new DomainExceptions\NotFoundException($message);
                break;

            default:
                throw new DomainExceptions\UnknownErrorException($message);
                break;
        }
    }
35682
private function sendErrorDocument(
        ResponseInterface $response): ResponseInterface
        {
        if ($this->errorDocument && preg_match(
                "/^(4|5){1}[0-2]{1}[0-9]{1}$/", $response->getStatusCode())
            ) {
            $this->errorDocument = str_replace(
                "{status}", $response->getStatusCode(), $this->errorDocument
            );
            if ($this->errorDocument !== filter_input(INPUT_SERVER, "REQUEST_URI")) {
                $response = $response->withHeader(
                    Response::HEADER_REFRESH,
                    "0; " . $this->errorDocument
                );
            }
        }
        return $response;
    }
749041
protected function handleErrorResponse( ResponseInterface $response ){

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

        $message = "HTTP:{$response->getStatusCode()}";

        throw new Exception\ApiException( $message, $response->getStatusCode(), $body, $response );

    }
417976
protected function checkResponse(ResponseInterface $response, $data)
  {
    if($response->getStatusCode() != 200 || !is_array($data))
    {
      $data = ValueAs::arr($data);
      $msg = Arrays::value($data, 'message', 'An unknown error occurred');
      throw new IdentityProviderException($msg, $response->getStatusCode(), $data);
    }
  }
866984
public function onError( ResponseInterface $response, RequestInterface $request ) {
        $json = $this->getResponseAsJson( $response );
        if( !is_null( $json ) && isset( $json->text )) {
            if( strpos( $json->text, 'page out of range.' ) === 0 ) {
                throw new PageOutOfRangeException( $json->text, $response );
            }
        }
    }
1299768
protected function checkResponse(ResponseInterface $response, $data)
    {
        $exception = null;

        if (isset($data['errors'])) {
            foreach ($data['errors'] as $error) {
                $exception = new YextException($error['message'], $error['errorCode'], $exception);
            }
        }

        if ($exception != null) {
            throw $exception;
        }
    }
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;
    }
552740
protected function checkResponse(ResponseInterface $response, $data)
    {
        if (! empty($data['error'])) {

            if (is_array($error = $data['error'])) {
                $code   = $error['code'];
                $error  = $error['message'];
            } else {
                $code   = 0;
                $error  = isset($data['error_description']) ? $data['error_description'] : $data['error'];
            }

            throw new IdentityProviderException($error, $code, $data);
        }
    }
856773
public function handleException(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
    {
        if ($level = $this->shouldLog('error')) {
            $this->logError($exception, $level);
        }

        return $this->handler->handleException($request, $response, $exception);
    }
378693
protected function handleErrorResponse(Response $response)
    {
        $exception = $response->exception;

        $error = new MessageBag([
            'type'    => get_class($exception),
            'message' => $exception->getMessage(),
            'file'    => $exception->getFile(),
            'line'    => $exception->getLine(),
        ]);

        return back()->withInput()->withErrors($error, 'exception');
    }
810229
private function processError(ResponseInterface $response, array $arguments): ResponseInterface
    {
        if (is_callable($this->options["error"])) {
            $handlerResponse = $this->options["error"]($response, $arguments);
            if ($handlerResponse instanceof ResponseInterface) {
                return $handlerResponse;
            }
        }
        return $response;
    }
785392
protected function exception(ConnectionInterface $connection, ErrorResponseInterface $response)
    {
        $connection->disconnect();
        $message = $response->getMessage();

        throw new ServerException($message);
    }
556449
private function setErrorFromResponse(Response $response)
    {
        $this->statusCode = $response->getStatus();
        $body = $response->getBody();
        if (isset($body['ErrorInfo'])) {
            $this->errorInfo = $body['ErrorInfo'];
        }
        if (isset($body['ErrorMessage'])) {
            $this->errorMessage = $body['ErrorMessage'];
        }
        if (isset($body['ErrorIdentifier'])) {
            $this->errorIdentifier = $body['ErrorIdentifier'];
        }
    }
892227
protected function handle(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $cors = $this->cors->getCors();
        $cors->setRequest('psr-7', $request);
        $cors->setResponse('psr-7', $response);
        $cors->handle();

        return $cors->getResponse();
    }
897305
protected function handleTokenResponse(ResponseInterface $response)
    {
        $statusCode = $response->getStatusCode();
        if (500 <= $statusCode) {
            $e = new ServiceUnavailableException();
            $e->setResponse($response);
            throw $e;
        }

        $contentTypes = $response->getHeader('content-type');
        if (empty($contentTypes) || (0 !== strpos($contentTypes[0], 'application/json') && 0 !== strpos($contentTypes[0], 'text/javascript'))) {
            $e = new UnknownContentTypeException();
            $e->setResponse($response);
            if (!empty($contentTypes)) {
                $e->setContentType($contentTypes[0]);
            }
            throw $e;
        }

        $data = json_decode($response->getBody()->getContents(), true);
        if (200 !== $statusCode) {
            $e = new RequestTokenException();
            $e->setResponse($response);

            if (isset($data['error'])) {
                $e->setError(is_array($data['error']) ? json_encode($data['error']) : $data['error']);
            }

            if (isset($data['error_description'])) {
                $e->setDescription($data['error_description']);
            }

            if (isset($data['error_uri'])) {
                $e->setUri($data['error_uri']);
            }

            throw $e;
        }

        if (isset($data['access_token'])) {
            $this->setAccessToken($data['access_token']);
        }

        return $data;
    }
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);
		}
	}
65577
protected function handleError($response) {
    $errorMsg = $response->getValue('errorMsg');
    self::$logger->error("Error in remote call to ".$this->serverCli.": ".$errorMsg."\n".$response->toString());
    throw new \RuntimeException("Error in remote call to ".$this->serverCli.": ".$errorMsg);
  }