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;
class ApiException extends Exception
{
protected $request;
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;
}
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);
}
public function getRequest()
{
return $this->request;
}
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 [];
}
public function getErrors()
{
$data = $this->getResponseJson();
return isset($data['errors']) ? $data['errors'] : [];
}
public function getResponseMessage()
{
$data = $this->getResponseJson();
return isset($data['message']) ? $data['message'] : '';
}
public function getResponseStatusCode()
{
$data = $this->getResponseJson();
return isset($data['statusCode']) ? $data['statusCode'] : '';
}
}