1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-17 21:00:09 -07:00

Add ApiExceptions

This commit is contained in:
David Hübner 2017-04-05 16:56:15 +02:00
parent 4c78346c89
commit 6fb797a124
7 changed files with 108 additions and 16 deletions

View File

@ -9,6 +9,10 @@ use AppBundle\Api\Client\Brickset\Entity\Set;
use AppBundle\Api\Client\Brickset\Entity\Subtheme;
use AppBundle\Api\Client\Brickset\Entity\Theme;
use AppBundle\Api\Client\Brickset\Entity\Year;
use AppBundle\Api\Exception\ApiException;
use AppBundle\Api\Exception\AuthenticationFailedException;
use AppBundle\Api\Exception\CallFailedException;
use AppBundle\Api\Exception\EmptyResponseException;
use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Debug\Exception\ContextErrorException;
@ -42,6 +46,8 @@ class Brickset extends \SoapClient
{
$this->apiKey = $apikey;
$options['cache_wsdl'] = WSDL_CACHE_NONE;
foreach (self::$classmap as $key => $value) {
if (!isset($options['classmap'][$key])) {
$options['classmap'][$key] = $value;
@ -66,13 +72,12 @@ class Brickset extends \SoapClient
$parameters['apiKey'] = $this->apiKey;
try {
$this->checkApiKey();
return $this->__soapCall($method, [$parameters])->{$method.'Result'};
} catch (\SoapFault $e) {
//TODO
throw new LogicException($e->getCode().' - '.$e->getMessage());
throw new CallFailedException(ApiException::BRICKSET);
} catch (ContextErrorException $e) {
//TODO empty resposne
throw new LogicException($method.' - '.$e->getMessage());
throw new EmptyResponseException(ApiException::BRICKSET);
}
}
@ -247,10 +252,14 @@ class Brickset extends \SoapClient
/**
* Check if an API key is valid.
*
* @return bool
* @throws AuthenticationFailedException If api key is not valid
*/
public function checkKey()
private function checkApiKey()
{
return ($this->call('checkKey', [])) == 'OK' ? true : false;
$parameters['apiKey'] = $this->apiKey;
if($this->__soapCall('checkKey', [$parameters])->checkKeyResult != 'OK') {
throw new AuthenticationFailedException(ApiException::BRICKSET);
}
}
}

View File

@ -14,7 +14,7 @@ class PropertyNameConverter implements NameConverterInterface
public function denormalize($propertyName)
{
switch ($propertyName) {
case 'part_num': return 'id';
case 'part_num': return 'number';
case 'part_cat_id': return 'categoryId';
case 'part_img_url': return 'imgUrl';
case 'part_url': return 'url';

View File

@ -2,6 +2,10 @@
namespace AppBundle\Api\Client\Rebrickable;
use AppBundle\Api\Exception\ApiException;
use AppBundle\Api\Exception\AuthenticationFailedException;
use AppBundle\Api\Exception\CallFailedException;
use AppBundle\Api\Exception\EmptyResponseException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
@ -51,17 +55,15 @@ class Rebrickable_v3
return $content;
} catch (ConnectException $e) {
//TODO
throw new LogicException($e);
throw new CallFailedException(ApiException::REBRICKABLE);
} catch (ClientException $e) {
//TODO
if ($e->getCode() == 404) {
throw new LogicException('Not Found');
throw new EmptyResponseException(ApiException::REBRICKABLE);
} else if ($e->getCode() == 401) {
throw new AuthenticationFailedException(ApiException::REBRICKABLE);
}
if ($e->getCode() == 500) {
throw new LogicException('Invalid token');
}
throw new LogicException($e);
throw new ApiException(ApiException::REBRICKABLE,$e,$e->getCode());
}
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace AppBundle\Api\Exception;
class ApiException extends \Exception
{
const BRICKSET = 'Brickset';
const REBRICKABLE = 'Rebrickable';
/**
* @var string
*/
private $service;
/**
* ApiException constructor.
*/
public function __construct($service = 'unknownService', $message = '', $code = 0, \Exception $previous = null)
{
parent::__construct($message, $code, $previous);
$this->service = $service;
}
/**
* @return string
*/
public function getService()
{
return $this->service;
}
/**
* @param string $service
*/
public function setService($service)
{
$this->service = $service;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace AppBundle\Api\Exception;
class AuthenticationFailedException extends ApiException
{
/**
* ApiException constructor.
*/
public function __construct($service)
{
parent::__construct($service, 'flash.authentication_failed');
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace AppBundle\Api\Exception;
class CallFailedException extends ApiException
{
/**
* ApiException constructor.
*/
public function __construct($service)
{
parent::__construct($service, 'flash.call_failed');
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace AppBundle\Api\Exception;
class EmptyResponseException extends ApiException
{
/**
* ApiException constructor.
*/
public function __construct($service)
{
parent::__construct($service, 'flash.empty_response');
}
}