Example #1
0
 /**
  * checkError
  *
  * This method will attempt to translate error messages from Guzzle
  *
  * @param \Exception $e            
  * @param iApiContext $apiContext            
  * @throws \Mozu\Api\ApiException
  * @throws ApiException
  */
 public static function checkError(\Exception $e, iApiContext $apiContext = null)
 {
     switch (get_class($e)) {
         case "GuzzleHttp\\Exception\\BadResponseException":
         case "GuzzleHttp\\Exception\\ClientErrorResponseException":
         case "GuzzleHttp\\Exception\\ServerErrorResponseException":
         case "GuzzleHttp\\Exception\\ServerException":
         case "GuzzleHttp\\Exception\\ClientException":
         case "GuzzleHttp\\Exception\\RequestException":
             $code = $e->getResponse()->getStatusCode();
             $response = $e->getResponse()->getBody(TRUE);
             $jsonResponse = json_decode($response);
             $apiException = null;
             if (!empty($jsonResponse)) {
                 if (isset($jsonResponse->message)) {
                     $apiException = new ApiException($jsonResponse->message, $code);
                 } else {
                     $apiException = new ApiException($e->getResponse()->getReasonPhrase() . ", inspect Mozu\\Api\\ApiException->items property for more details", $code);
                 }
                 if (isset($jsonResponse->additionalErrorData)) {
                     $apiException->setAdditionalErrorData($jsonResponse->additionalErrorData);
                 }
                 if (isset($jsonResponse->errorCode)) {
                     $apiException->setErrorCode($jsonResponse->errorCode);
                 }
                 if (isset($jsonResponse->applicationName)) {
                     $apiException->setApplicationName($jsonResponse->applicationName);
                 }
                 if (isset($jsonResponse->items)) {
                     $apiException->setItems($jsonResponse->items);
                 }
             } else {
                 $apiException = new ApiException($e->getResponse()->getReasonPhrase(), $code);
             }
             $correlation = $e->getResponse()->getHeader("x-vol-correlation");
             if (!empty($correlation)) {
                 $apiException->setCorrelationId($correlation[0]);
             }
             throw $apiException;
             break;
         default:
             throw $e;
     }
 }
Example #2
0
 private function validateResponse(ResponseInterface $response)
 {
     $statusCode = $response->getStatusCode();
     $correlationHdr = $response->getHeader(Headers::X_VOL_CORRELATION);
     if ($statusCode == 304 || $statusCode >= 200 && $statusCode <= 300) {
         return;
     }
     $apiError = null;
     if ($statusCode == 404 && empty($correlationHdr)) {
         $apiError = new ApiException($response->getReasonPhrase(), $statusCode);
     } else {
         if (!empty($correlationHdr)) {
             $body = $response->getBody(TRUE);
             $jsonResponse = json_decode($body);
             $message = $response->getReasonPhrase();
             if (!empty($jsonResponse)) {
                 if ($jsonResponse->errorCode == "ITEM_NOT_FOUND" && $statusCode == 404 && $this->mozuUrl->getVerb() == "GET" && !MozuConfig::$throwExceptionOn404) {
                     $this->notFoundFromServer = true;
                     return;
                 }
                 $this->logger->debug($jsonResponse->errorCode . " : " . (MozuConfig::$throwExceptionOn404 == true));
                 if (isset($jsonResponse->message)) {
                     $apiError = new ApiException($jsonResponse->message, $statusCode);
                 } else {
                     $apiError = new ApiException($response->getReasonPhrase() . ", inspect Mozu\\Api\\ApiException->items property for more details", $statusCode);
                 }
                 $apiError = new ApiException($jsonResponse->message, $statusCode);
                 if (isset($jsonResponse->additionalErrorData)) {
                     $apiError->setAdditionalErrorData($jsonResponse->additionalErrorData);
                 }
                 if (isset($jsonResponse->errorCode)) {
                     $apiError->setErrorCode($jsonResponse->errorCode);
                 }
                 if (isset($jsonResponse->applicationName)) {
                     $apiError->setApplicationName($jsonResponse->applicationName);
                 }
                 if (isset($jsonResponse->items)) {
                     $apiError->setItems($jsonResponse->items);
                 }
             } else {
                 $apiError = new ApiException($message, $statusCode);
             }
             $apiError->setCorrelationId($correlationHdr[0]);
         } else {
             $apiError = new ApiException("Unknown error", $statusCode);
         }
     }
     if ($apiError != null) {
         throw $apiError;
     }
 }