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