1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AcquiaCloudApi\Exception; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Represents an error returned from the API. |
9
|
|
|
*/ |
10
|
|
|
class ApiErrorException extends Exception |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var object |
15
|
|
|
*/ |
16
|
|
|
private $responseBody; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ApiErrorException Constructor. |
20
|
|
|
* |
21
|
|
|
* @param object $response_body |
22
|
|
|
* @param string $message |
23
|
|
|
* @param int $code |
24
|
|
|
* @param Exception $previous |
25
|
|
|
*/ |
26
|
|
|
public function __construct($response_body, $message = "", $code = 0, Exception $previous = null) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($message, $code, $previous); |
29
|
|
|
|
30
|
|
|
$this->setResponseBody($response_body); |
31
|
|
|
$this->setError($response_body); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* __toString() magic method. |
36
|
|
|
*/ |
37
|
|
|
public function __toString() |
38
|
|
|
{ |
39
|
|
|
return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sets message and code properties. |
44
|
|
|
* |
45
|
|
|
* @param object $response_body |
46
|
|
|
*/ |
47
|
|
|
public function setError($response_body) |
48
|
|
|
{ |
49
|
|
|
if (is_array($response_body->message) || is_object($response_body->message)) { |
50
|
|
|
$output = ''; |
51
|
|
|
foreach ($response_body->message as $message) { |
52
|
|
|
$output .= $message . PHP_EOL; |
53
|
|
|
} |
54
|
|
|
$this->message = $output; |
55
|
|
|
} else { |
56
|
|
|
$this->code = $response_body->error; |
57
|
|
|
$this->message = $response_body->message; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return object |
63
|
|
|
*/ |
64
|
|
|
public function getResponseBody() |
65
|
|
|
{ |
66
|
|
|
return $this->responseBody; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param object $response_body |
71
|
|
|
*/ |
72
|
|
|
private function setResponseBody($response_body) |
73
|
|
|
{ |
74
|
|
|
$this->responseBody = $response_body; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|