Composable Commerce PHP SDKPHP SDK
  • Namespace
  • Class
  • Tree

Namespaces

  • Commercetools
    • Commons
      • Helper
    • Core
      • Builder
        • Request
        • Update
      • Cache
      • Client
        • Adapter
        • OAuth
      • Error
      • Helper
        • Annotate
        • State
          • Renderer
        • Subscriber
          • Log
      • Model
        • ApiClient
        • Cart
        • CartDiscount
        • Category
        • Channel
        • Common
        • Customer
        • CustomerGroup
        • CustomField
        • CustomObject
        • DiscountCode
        • Extension
        • Inventory
        • Message
        • Order
        • OrderEdit
        • Payment
        • Product
          • Search
        • ProductDiscount
        • ProductSelection
        • ProductType
        • Project
        • Review
        • ShippingMethod
        • ShoppingList
        • State
        • Store
        • Subscription
        • TaxCategory
        • Type
        • Zone
      • Request
        • ApiClients
        • CartDiscounts
          • Command
        • Carts
          • Command
        • Categories
          • Command
        • Channels
          • Command
        • CustomerGroups
          • Command
        • Customers
          • Command
        • CustomField
          • Command
        • CustomObjects
        • DiscountCodes
          • Command
        • Extensions
          • Command
        • GraphQL
        • InStores
        • Inventory
          • Command
        • Me
          • Command
        • Messages
        • OrderEdits
          • Command
          • StagedOrder
            • Command
        • Orders
          • Command
        • Payments
          • Command
        • ProductDiscounts
          • Command
        • Products
          • Command
        • ProductSelections
          • Command
        • ProductTypes
          • Command
        • Project
          • Command
        • Query
        • Reviews
          • Command
        • ShippingMethods
          • Command
        • ShoppingLists
          • Command
        • States
          • Command
        • Stores
          • Command
        • Subscriptions
          • Command
        • TaxCategories
          • Command
        • Types
          • Command
        • Zones
          • Command
      • Response

Classes

  • AccessDeniedError
  • ApiError
  • ConcurrentModificationError
  • DiscountCodeNonApplicableError
  • DuplicateAttributeValueError
  • DuplicateAttributeValuesError
  • DuplicateFieldError
  • DuplicateFieldWithConflictingResourceError
  • DuplicatePriceScopeError
  • DuplicateVariantValuesError
  • EnumValueIsUsedError
  • ErrorContainer
  • InsufficientScopeError
  • InvalidCredentialsError
  • InvalidCurrentPasswordError
  • InvalidFieldError
  • InvalidItemShippingDetailsError
  • InvalidOperationError
  • InvalidSubjectError
  • InvalidTokenError
  • LanguageUsedInStoresError
  • MatchingPriceNotFoundError
  • Message
  • MissingRoleOnChannelError
  • MissingTaxRateForCountryError
  • NoMatchingProductDiscountFoundError
  • OutOfStockError
  • PriceChangedError
  • ProjectNotConfiguredForLanguagesError
  • QueryTimedOutError
  • RequiredFieldError
  • ResourceNotFoundError
  • ShippingMethodDoesNotMatchCartError

Exceptions

  • ApiException
  • ApiServiceException
  • BadGatewayException
  • BadRequestException
  • ClientErrorException
  • ConcurrentModificationException
  • DeprecatedException
  • ErrorResponseException
  • ForbiddenException
  • GatewayTimeoutException
  • InternalServerErrorException
  • InvalidArgumentException
  • InvalidClientCredentialsException
  • InvalidTokenException
  • NotFoundException
  • ServerErrorException
  • ServiceUnavailableException
  • UnauthorizedException
  • UpdateActionLimitException
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 
<?php

namespace Commercetools\Core\Error;

use Exception;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * Base exception for responses with http status code different than 200 or 201
 * @package Commercetools\Core\Error
 */
class ApiException extends Exception
{
    /**
     * @var RequestInterface
     */
    protected $request;

    /**
     * @var ResponseInterface
     */
    protected $response;

    public function __construct(
        $message,
        RequestInterface $request,
        ResponseInterface $response = null,
        Exception $previous = null
    ) {
        $code = $response ? $response->getStatusCode() : ($previous ? $previous->getCode() : 0);
        parent::__construct($message, $code, $previous);
        $this->request = $request;
        $this->response = $response;
    }

    /**
     * @param RequestInterface $request
     * @param ResponseInterface|null $response
     * @param Exception|null $previous
     */
    public static function create(
        RequestInterface $request,
        ResponseInterface $response = null,
        Exception $previous = null
    ) {
        if (is_null($response)) {
            $message = $previous ? 'Error completing request: ' . $previous->getMessage() : "Error completing request";
            return new self($message, $request, null, $previous);
        }

        $level = floor($response->getStatusCode() / 100);
        if ($level == 4) {
            $label = 'Client error response';
        } elseif ($level == 5) {
            $label = 'Server error response';
        } else {
            $label = 'Unsuccessful response';
        }

        $message = $label . ' [url] ' . $request->getUri()
             . ' [status code] ' . $response->getStatusCode()
             . ' [reason phrase] ' . $response->getReasonPhrase();

        switch ($response->getStatusCode()) {
            case 400:
                if ($response->getBody()->getSize() > 0) {
                    $body = (string)$response->getBody()->getContents();
                    $response = $response->withBody(Utils::streamFor($body));
                    $message .= ' [body] ' . $body;
                }
                return new ErrorResponseException($message, $request, $response, $previous);
            case 401:
                $body = $response->getBody()->getContents();
                $response = $response->withBody(Utils::streamFor($body));
                if (strpos($body, 'invalid_token') !== false) {
                    return new InvalidTokenException($message, $request, $response, $previous);
                }
                return new InvalidClientCredentialsException($message, $request, $response, $previous);
            case 403:
                return new ForbiddenException($message, $request, $response, $previous);
            case 404:
                return new NotFoundException($message, $request, $response, $previous);
            case 409:
                return new ConcurrentModificationException($message, $request, $response, $previous);
            case 500:
                return new InternalServerErrorException($message, $request, $response, $previous);
            case 502:
                return new BadGatewayException($message, $request, $response, $previous);
            case 503:
                return new ServiceUnavailableException($message, $request, $response, $previous);
            case 504:
                return new GatewayTimeoutException($message, $request, $response, $previous);
        }

        return new self($message, $request, $response, $previous);
    }

    /**
     * @return RequestInterface
     */
    public function getRequest()
    {
        return $this->request;
    }

    /**
     * @return ResponseInterface
     */
    public function getResponse()
    {
        return $this->response;
    }

    protected function getResponseJson()
    {
        $response = $this->getResponse();
        if ($response instanceof ResponseInterface) {
            $json = json_decode($response->getBody(), true);
            return $json;
        }

        return [];
    }

    /**
     * @return array
     */
    public function getErrors()
    {
        $data = $this->getResponseJson();

        return isset($data['errors']) ? $data['errors'] : [];
    }

    /**
     * @return string
     */
    public function getResponseMessage()
    {
        $data = $this->getResponseJson();

        return isset($data['message']) ? $data['message'] : '';
    }

    /**
     * @return string
     */
    public function getResponseStatusCode()
    {
        $data = $this->getResponseJson();

        return isset($data['statusCode']) ? $data['statusCode'] : '';
    }
}
PHP SDK API documentation generated by ApiGen