|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Updivision\Matrix\Exceptions; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use GuzzleHttp\Exception\RequestException; |
| 7 | + |
| 8 | +/** |
| 9 | + * General Exception |
| 10 | + * |
| 11 | + * Thrown when the Matrix API returns an HTTP error code that isn't handled by other exceptions |
| 12 | + * |
| 13 | + * @package Exceptions |
| 14 | + * @author Alin Ghitu <[email protected]> |
| 15 | + */ |
| 16 | +class ApiException extends Exception |
| 17 | +{ |
| 18 | + |
| 19 | + /** |
| 20 | + * @internal |
| 21 | + * @param RequestException $e |
| 22 | + * @return AccessDeniedException|ApiException|AuthenticationException|ConflictingStateException| |
| 23 | + * MethodNotAllowedException|NotFoundException|RateLimitExceededException|UnsupportedAcceptHeaderException| |
| 24 | + * UnsupportedContentTypeException|ValidationException |
| 25 | + */ |
| 26 | + public static function create(RequestException $e) { |
| 27 | + |
| 28 | + if($response = $e->getResponse()) { |
| 29 | + switch ($response->getStatusCode()) { |
| 30 | + case 400: |
| 31 | + return new ValidationException($e); |
| 32 | + case 401: |
| 33 | + return new AuthenticationException($e); |
| 34 | + case 403: |
| 35 | + return new AccessDeniedException($e); |
| 36 | + case 404: |
| 37 | + return new NotFoundException($e); |
| 38 | + case 405: |
| 39 | + return new MethodNotAllowedException($e); |
| 40 | + case 406: |
| 41 | + return new UnsupportedAcceptHeaderException($e); |
| 42 | + case 409: |
| 43 | + return new ConflictingStateException($e); |
| 44 | + case 415: |
| 45 | + return new UnsupportedContentTypeException($e); |
| 46 | + case 429: |
| 47 | + return new RateLimitExceededException($e); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return new ApiException($e); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @var RequestException |
| 56 | + * @internal |
| 57 | + */ |
| 58 | + private $exception; |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the Request Exception |
| 62 | + * |
| 63 | + * A Guzzle Request Exception is returned |
| 64 | + * |
| 65 | + * @return RequestException |
| 66 | + */ |
| 67 | + public function getRequestException() |
| 68 | + { |
| 69 | + return $this->exception; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Exception constructor |
| 74 | + * |
| 75 | + * Constructs a new exception. |
| 76 | + * |
| 77 | + * @param RequestException $e |
| 78 | + * @internal |
| 79 | + */ |
| 80 | + public function __construct(RequestException $e) |
| 81 | + { |
| 82 | + $this->exception = $e; |
| 83 | + parent::__construct(); |
| 84 | + } |
| 85 | +} |
0 commit comments