/**
  * @param   RequestException $ex
  * @return  AccessDeniedException|EntityNotFoundException|EntityValidationException|RequiredFieldMissingException|UnauthorizedClientException|CaravanaHttpException
  */
 public static function parseRequestException(RequestException $ex)
 {
     $code = $ex->getCode();
     $body = json_decode($ex->getResponse()->getBody()->getContents(), true);
     if (CaravanaExceptionFactory::isEntityValidationException($code, $body)) {
         return new EntityValidationException(AU::get($body['Entity']), AU::get($body['field']), AU::get($body['reason']), AU::get($body['providedValue']), $ex->getPrevious());
     } else {
         if (CaravanaExceptionFactory::isRequiredFieldMissingException($code, $body)) {
             return new RequiredFieldMissingException(AU::get($body['Entity']), AU::get($body['field']), $ex->getPrevious());
         } else {
             if (CaravanaExceptionFactory::isUnauthorizedClientException($code, $body)) {
                 return new UnauthorizedClientException($ex->getMessage(), $ex->getPrevious());
             } else {
                 if (CaravanaExceptionFactory::isAccessDeniedException($code, $body)) {
                     return new AccessDeniedException($ex->getMessage(), $ex->getPrevious());
                 } else {
                     if (CaravanaExceptionFactory::isEntityNotFoundException($code, $body)) {
                         return new EntityNotFoundException(AU::get($body['Entity']), AU::get($body['field']), AU::get($body['providedValue']), $ex->getPrevious());
                     }
                 }
             }
         }
     }
     //  Give up and default it to CaravanaException
     return new CaravanaHttpException($ex->getMessage(), $ex->getCode(), null, $ex->getPrevious());
 }
 /**
  * @param       bool            $returnResponse     If true the CreateAccessTokenResponse will be returned
  * @return      CreateAccessTokenResponse
  * @throws      RequiredFieldMissingException|UnauthorizedClientException
  */
 public function requestAccessToken($returnResponse = false)
 {
     try {
         $accessTokenResponse = $this->api->requestAccessToken($returnResponse);
         if ($returnResponse) {
             return $accessTokenResponse;
         } else {
             return null;
         }
     } catch (RequestException $ex) {
         $caravanaException = CaravanaExceptionFactory::parseRequestException($ex);
         throw $caravanaException;
     }
 }