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
|
|
|
private object $responseBody; |
13
|
|
|
|
14
|
|
|
private string $errorType; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ApiErrorException Constructor. |
18
|
|
|
*/ |
19
|
|
|
public function __construct(object $response_body, string $message = "", int $code = 0, ?Exception $previous = null) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($message, $code, $previous); |
22
|
|
|
|
23
|
|
|
$this->setResponseBody($response_body); |
24
|
|
|
$this->setError($response_body); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* __toString() magic method. |
29
|
|
|
*/ |
30
|
|
|
public function __toString(): string |
31
|
|
|
{ |
32
|
|
|
return __CLASS__ . ": [{$this->errorType}]: {$this->message}\n"; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Sets message and errorType properties. |
37
|
|
|
*/ |
38
|
|
|
private function setError(object $response_body): void |
39
|
|
|
{ |
40
|
|
|
if (is_array($response_body->message) || is_object($response_body->message)) { |
41
|
|
|
$output = ''; |
42
|
|
|
foreach ($response_body->message as $message) { |
43
|
|
|
$output .= $message . PHP_EOL; |
44
|
|
|
} |
45
|
|
|
$this->message = $output; |
46
|
|
|
} else { |
47
|
|
|
$this->errorType = $response_body->error; |
48
|
|
|
$this->message = $response_body->message; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getResponseBody(): object |
53
|
|
|
{ |
54
|
|
|
return $this->responseBody; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function setResponseBody(object $response_body): void |
58
|
|
|
{ |
59
|
|
|
$this->responseBody = $response_body; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|