Skip to content

Commit

Permalink
Merge pull request #5 from polidog/exception
Browse files Browse the repository at this point in the history
エラー処理の変更
  • Loading branch information
polidog committed Oct 21, 2017
2 parents cf5456d + 740fba4 commit 195ecd6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Polidog\Esa;

use GuzzleHttp\ClientInterface as HttpClientInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Polidog\Esa\Exception\ApiErrorException;
use Psr\Http\Message\RequestInterface;

final class Client
Expand Down Expand Up @@ -51,12 +53,22 @@ public function __construct($accessToken, $currentTeam, HttpClientInterface $htt
$this->apiMethods = new ApiMethods($httpClient, $currentTeam);
}

/**
* @param $name
* @param $args
* @return mixed
*
* @throws ApiErrorException
*/
public function __call($name, $args)
{
/** @var Response $response */
$response = call_user_func_array([$this->apiMethods, $name], $args);
if ($response->getStatusCode() == 200) {
return json_decode($response->getBody()->getContents(), true);
try {
/** @var Response $response */
$response = call_user_func_array([$this->apiMethods, $name], $args);
$data = json_decode($response->getBody()->getContents(), true);
return $data;
} catch (\Exception $exception) {
throw ApiErrorException::newException($exception, $name, $args);
}
}

Expand Down
48 changes: 48 additions & 0 deletions src/Exception/ApiErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Polidog\Esa\Exception;

use GuzzleHttp\Exception\ClientException;

class ApiErrorException extends \RuntimeException
{
/**
* @var string
*/
private $apiMethodName;

/**
* @var array
*/
private $args;

/**
* @return string
*/
public function getApiMethodName()
{
return $this->apiMethodName;
}

/**
* @return array
*/
public function getArgs()
{
return $this->args;
}

/**
* @param \Exception $e
* @param $name
* @param array $args
* @return ApiErrorException
*/
public static function newException(\Exception $e, $name, array $args)
{
$self = new self(sprintf("Api method error: %s", $name), $e->getCode(), $e);
$self->apiMethodName = $name;
$self->args = $args;
return $self;
}

}
4 changes: 0 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ public function callApi()
Phake::when($httpClient)->request('GET', 'teams')
->thenReturn($response);

Phake::when($response)->getStatusCode()
->thenReturn(200);

Phake::when($response)->getBody()
->thenReturn($stream);

$client = new Client('token', 'polidog', $httpClient);
$client->teams();

Phake::verify($response)->getStatusCode();
Phake::verify($response)->getBody();
Phake::verify($stream)->getContents();
}
Expand Down

0 comments on commit 195ecd6

Please sign in to comment.