Skip to content

Commit

Permalink
Use full body content as exception message when catching ClientExcept…
Browse files Browse the repository at this point in the history
…ion (with Guzzle 6)
  • Loading branch information
maclof committed Apr 19, 2018
1 parent bb0bfa8 commit 0e34e6b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
29 changes: 21 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Client
'events' => 'Repositories\EventRepository',
'configMaps' => 'Repositories\ConfigMapRepository',
'endpoints' => 'Repositories\EndpointRepository',
'persistentVolume' => 'Repositories\PersistentVolumeRepository',
'persistentVolume' => 'Repositories\PersistentVolumeRepository',
'persistentVolumeClaims' => 'Repositories\PersistentVolumeClaimRepository',
'namespaces' => 'Repositories\NamespaceRepository',

Expand Down Expand Up @@ -312,7 +312,7 @@ public function getGuzzleClient()
* @param boolean $namespace
* @param string $apiVersion
* @return array|string
* @throws \Exception
* @throws \Maclof\Kubernetes\Exceptions\BadRequestException
*/
public function sendRequest($method, $uri, $query = [], $body = [], $namespace = true, $apiVersion = null)
{
Expand All @@ -339,7 +339,7 @@ public function sendRequest($method, $uri, $query = [], $body = [], $namespace =
$request = $this->guzzleClient->createRequest($method, $requestUri, $requestOptions);
$response = $this->guzzleClient->send($request);
} catch (ClientException $e) {
throw new BadRequestException($e->getMessage());
throw new BadRequestException($e->getMessage(), 0, $e);
}

try {
Expand All @@ -349,15 +349,28 @@ public function sendRequest($method, $uri, $query = [], $body = [], $namespace =
}
}

$response = $this->guzzleClient->request($method, $requestUri, $requestOptions);
try {
$response = $this->guzzleClient->request($method, $requestUri, $requestOptions);

$bodyResponse = (string) $response->getBody();
$jsonResponse = json_decode($bodyResponse, true);
$bodyResponse = (string) $response->getBody();
$jsonResponse = json_decode($bodyResponse, true);

return is_array($jsonResponse) ? $jsonResponse : $bodyResponse;
return is_array($jsonResponse) ? $jsonResponse : $bodyResponse;
} catch (ClientException $e) {
$fullMessage = (string) $e->getResponse()->getBody();
throw new BadRequestException($fullMessage, 0, $e);
}
}

public function __call($name, $args)
/**
* Magic call method to grab a class instance.
*
* @param string $name
* @param array $args
* @return \stdClass
* @throws \BadMethodCallException
*/
public function __call($name, array $args)
{
if (isset($this->classMap[$name])) {
$class = 'Maclof\Kubernetes\\' . $this->classMap[$name];
Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/BadRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class BadRequestException extends Exception
{
}

}
4 changes: 2 additions & 2 deletions src/Exceptions/MissingOptionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class MissingOptionException extends Exception
{
}

}

0 comments on commit 0e34e6b

Please sign in to comment.