commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
ExceptionFactory.php
1 <?php
2 
3 declare(strict_types=1);
10 namespace Commercetools\Exception;
11 
14 use GuzzleHttp\Exception\ClientException;
15 use GuzzleHttp\Exception\ServerException;
16 use GuzzleHttp\Psr7\Response;
17 use Psr\Http\Message\ResponseInterface;
18 
20 {
21  public static function createServerException(
22  ServerException $e,
23  ApiRequest $request,
24  ?ResponseInterface $response,
25  ?JsonObject $result
27  if (is_null($response)) {
28  $message = 'Error completing request: ' . $e->getMessage();
29  return new ApiServerException($message, null, $request, new Response(400), $e, []);
30  }
31 
32  $message = 'Server error response [url] ' . (string)$request->getUri()
33  . ' [status code] ' . (string)$response->getStatusCode()
34  . ' [reason phrase] ' . $response->getReasonPhrase();
35 
36  switch ($response->getStatusCode()) {
37  case 500:
38  return new InternalServerErrorException($message, $result, $request, $response, $e, []);
39  case 502:
40  return new BadGatewayException($message, $result, $request, $response, $e, []);
41  case 503:
42  return new ServiceUnavailableException($message, $result, $request, $response, $e, []);
43  case 504:
44  return new GatewayTimeoutException($message, $result, $request, $response, $e, []);
45  }
46 
47  return new ApiServerException($message, $result, $request, $response, $e, []);
48  }
49 
50  public static function createClientException(
51  ClientException $e,
52  ApiRequest $request,
53  ?ResponseInterface $response,
54  ?JsonObject $result
56  if (is_null($response)) {
57  $message = 'Error completing request: ' . $e->getMessage();
58  return new ApiClientException($message, null, $request, new Response(400), $e, []);
59  }
60 
61  $message = 'Client error response [url] ' . (string)$request->getUri()
62  . ' [status code] ' . (string)$response->getStatusCode()
63  . ' [reason phrase] ' . $response->getReasonPhrase();
64 
65  switch ($response->getStatusCode()) {
66  case 400:
67  return new BadRequestException($message, $result, $request, $response, $e, []);
68  case 401:
69  return new UnauthorizedException($message, $result, $request, $response, $e, []);
70  case 403:
71  return new ForbiddenException($message, $result, $request, $response, $e, []);
72  case 404:
73  return new NotFoundException($message, $result, $request, $response, $e, []);
74  case 409:
75  return new ConcurrentModificationException($message, $result, $request, $response, $e, []);
76  }
77 
78  return new ApiClientException($message, $result, $request, $response, $e, []);
79  }
80 }
static createClientException(ClientException $e, ApiRequest $request, ?ResponseInterface $response, ?JsonObject $result)
static createServerException(ServerException $e, ApiRequest $request, ?ResponseInterface $response, ?JsonObject $result)