Skip to content

Commit 37d133f

Browse files
committed
improved exceptions
1 parent 35880e2 commit 37d133f

8 files changed

+58
-17
lines changed

src/Exceptions/KubeConfigClusterNotFound.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace RenokiCo\PhpK8s\Exceptions;
44

5-
use Exception;
6-
7-
class KubeConfigClusterNotFound extends Exception
5+
class KubeConfigClusterNotFound extends PhpK8sException
86
{
97
//
108
}

src/Exceptions/KubeConfigContextNotFound.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace RenokiCo\PhpK8s\Exceptions;
44

5-
use Exception;
6-
7-
class KubeConfigContextNotFound extends Exception
5+
class KubeConfigContextNotFound extends PhpK8sException
86
{
97
//
108
}

src/Exceptions/KubeConfigUserNotFound.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace RenokiCo\PhpK8s\Exceptions;
44

5-
use Exception;
6-
7-
class KubeConfigUserNotFound extends Exception
5+
class KubeConfigUserNotFound extends PhpK8sException
86
{
97
//
108
}

src/Exceptions/KubernetesAPIException.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace RenokiCo\PhpK8s\Exceptions;
44

5-
use Exception;
6-
7-
class KubernetesAPIException extends Exception
5+
class KubernetesAPIException extends PhpK8sException
86
{
97
//
108
}

src/Exceptions/KubernetesWatchException.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace RenokiCo\PhpK8s\Exceptions;
44

5-
use Exception;
6-
7-
class KubernetesWatchException extends Exception
5+
class KubernetesWatchException extends PhpK8sException
86
{
97
//
108
}

src/Exceptions/PhpK8sException.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace RenokiCo\PhpK8s\Exceptions;
4+
5+
use Exception;
6+
7+
class PhpK8sException extends Exception
8+
{
9+
/**
10+
* The payload coming from the Guzzle client.
11+
*
12+
* @var array
13+
*/
14+
protected $payload = [];
15+
16+
/**
17+
* Initialize the exception.
18+
*
19+
* @param string|null $message
20+
* @param int $code
21+
* @param array|null $payload
22+
*/
23+
public function __construct($message = null, $code = 0, array $payload = null)
24+
{
25+
parent::__construct($message, $code);
26+
27+
$this->payload = $payload;
28+
}
29+
30+
/**
31+
* Get the payload instance.
32+
*
33+
* @return null|array
34+
*/
35+
public function getPayload()
36+
{
37+
return $this->payload;
38+
}
39+
}

src/Traits/Cluster/ChecksClusterVersion.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ protected function loadClusterVersion(): void
3434
try {
3535
$response = $this->getClient()->request('GET', $callableUrl);
3636
} catch (ClientException $e) {
37-
throw new KubernetesAPIException($e->getMessage());
37+
$payload = json_decode((string) $e->getResponse()->getBody(), true);
38+
39+
throw new KubernetesAPIException(
40+
$e->getMessage(),
41+
$payload['code'] ?? 0,
42+
$payload
43+
);
3844
}
3945

4046
$json = @json_decode($response->getBody(), true);

src/Traits/Cluster/RunsClusterOperations.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ protected function makeRequest(string $method, string $path, string $payload = '
106106
RequestOptions::BODY => $payload,
107107
]);
108108
} catch (ClientException $e) {
109-
throw new KubernetesAPIException($e->getMessage());
109+
$payload = json_decode((string) $e->getResponse()->getBody(), true);
110+
111+
throw new KubernetesAPIException(
112+
$e->getMessage(),
113+
$payload['code'] ?? 0,
114+
$payload
115+
);
110116
}
111117

112118
$json = @json_decode($response->getBody(), true);

0 commit comments

Comments
 (0)