Skip to content

Commit fcb6efc

Browse files
committed
better error Handling
1 parent 979c4b0 commit fcb6efc

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/APIException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class APIException extends \Exception
1313
{
1414
/**
15-
* @var \Psr\Http\Message\ResponseInterface
15+
* @var ApiResponse
1616
*/
1717
public $response;
1818

@@ -24,7 +24,7 @@ class APIException extends \Exception
2424
* @param int $code
2525
* @param \Throwable|null $previous
2626
*/
27-
public function __construct(ResponseInterface $response, string $message = "", int $code = 0, \Throwable $previous = null)
27+
public function __construct(ApiResponse $response, string $message = "", int $code = 0, \Throwable $previous = null)
2828
{
2929
$this->response = $response;
3030
parent::__construct($message, $code, $previous);

src/HetznerAPIClient.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,16 @@ public function getHttpClient(): GuzzleClient
100100
*/
101101
public static function throwError(ResponseInterface $response)
102102
{
103-
var_dump(json_decode((string)$response->getBody()));
104-
die();
105-
// throw new APIException($response, ->error->code);
103+
$body = (string)$response->getBody();
104+
if (strlen($body) > 0) {
105+
$error = \GuzzleHttp\json_decode($body);
106+
throw new APIException(ApiResponse::create([
107+
'error' => $error->error
108+
]), $error->error->message);
109+
}
110+
throw new APIException(ApiResponse::create([
111+
'response' => $response
112+
]), 'The response is not parseable');
106113
}
107114

108115
/**
@@ -120,13 +127,15 @@ public static function hasError(ResponseInterface $response)
120127

121128
return false;
122129
}
130+
123131
/**
124132
* @return Actions
125133
*/
126134
public function actions()
127135
{
128136
return new Actions($this->httpClient);
129137
}
138+
130139
/**
131140
* @return Servers
132141
*/
@@ -138,9 +147,11 @@ public function servers()
138147
/**
139148
* @return ServerTypes
140149
*/
141-
public function serverTypes(){
150+
public function serverTypes()
151+
{
142152
return new ServerTypes($this->httpClient);
143153
}
154+
144155
/**
145156
* @return Datacenters
146157
*/

0 commit comments

Comments
 (0)