Skip to content

Commit

Permalink
Explicit Exceptions (#20)
Browse files Browse the repository at this point in the history
* added exceptions

* replaced generic exceptions with more specific ones
  • Loading branch information
infurio committed Jan 13, 2021
1 parent 6816cf8 commit 6ba6a39
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 26 deletions.
57 changes: 35 additions & 22 deletions src/Provider/Amqp/AmqpQueueProvider.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php
namespace Packaged\Queue\Provider\Amqp;

use Exception;
use Packaged\Queue\IBatchQueueProvider;
use Packaged\Queue\Provider\AbstractQueueProvider;
use Packaged\Queue\Provider\QueueConnectionException;
use Packaged\Queue\QueueException;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Connection\AMQPStreamConnection;
Expand Down Expand Up @@ -91,6 +94,13 @@ public function setSlowPushThreshold($threshold)
return $this;
}

/**
* @param array $batch
* @param null $persistent
*
* @return $this
* @throws QueueConnectionException
*/
public function pushBatch(array $batch, $persistent = null)
{
$mandatory = $this->_getMandatoryFlag();
Expand All @@ -106,27 +116,17 @@ public function pushBatch(array $batch, $persistent = null)
$returnCallback = null;
if($mandatory)
{
$returnCallback = function (
$replyCode,
$replyText,
$exchange,
$routingKey
) use (
&$needRetry, &$needDeclare, &$autoDeclare,
$declareAttempts, $declareRetryLimit
)
$returnCallback = function ($replyCode, $replyText, $exchange, $routingKey) use
(&$needRetry, &$needDeclare, &$autoDeclare, $declareAttempts, $declareRetryLimit)
{
if($autoDeclare
&& ($declareAttempts < $declareRetryLimit)
&& ($replyCode == 312)
)
if($autoDeclare && ($declareAttempts < $declareRetryLimit) && ($replyCode == 312))
{
$needDeclare = true;
$needRetry = true;
}
else
{
throw new \Exception(
throw new QueueConnectionException(
'Error pushing message to exchange ' . $exchange
. ' with routing key ' . $routingKey
. ' : (' . $replyCode . ') ' . $replyText,
Expand Down Expand Up @@ -178,7 +178,7 @@ public function pushBatch(array $batch, $persistent = null)
{
$ch->wait_for_pending_acks_returns($this->_getPushTimeout());
}
catch(\Exception $e)
catch(Exception $e)
{
$this->disconnect(self::CONN_PUSH);
if($autoDeclare
Expand Down Expand Up @@ -469,6 +469,10 @@ protected function _refreshConnection($connectionMode)
}
}

/**
* @return array
* @throws QueueConnectionException
*/
protected function _getHosts()
{
if(!$this->_hosts)
Expand All @@ -487,7 +491,7 @@ protected function _getHosts()
}
else
{
throw new \Exception(
throw new QueueConnectionException(
'All hosts failed to connect ' . $this->_hostsRetriesMax .
' times within ' . $this->_hostsResetTimeMax . ' seconds'
);
Expand All @@ -501,6 +505,7 @@ protected function _getHosts()
* @param $connectionMode
*
* @return AMQPStreamConnection
* @throws QueueConnectionException
*/
protected function _getConnection($connectionMode)
{
Expand All @@ -518,7 +523,7 @@ protected function _getConnection($connectionMode)
$config->getItem('password', 'guest')
);
}
catch(\Exception $e)
catch(Exception $e)
{
$this->_log('AMQP host failed to connect (' . $host . ')');
array_shift($this->_hosts);
Expand All @@ -536,7 +541,8 @@ protected function _getConnection($connectionMode)
* @param $connectionMode
*
* @return AMQPChannel
* @throws \Exception
* @throws QueueConnectionException
* @throws Exception
*/
protected function _getChannel($connectionMode)
{
Expand Down Expand Up @@ -565,7 +571,7 @@ protected function _getChannel($connectionMode)
break;
}
}
catch(\Exception $e)
catch(Exception $e)
{
$this->_log(
'Error getting AMQP channel (' . $retries . ' retries remaining)'
Expand Down Expand Up @@ -620,7 +626,7 @@ private function _disconnect($connectionMode)
$this->_channels[$connectionMode]->close();
}
}
catch(\Exception $e)
catch(Exception $e)
{
}
$this->_channels[$connectionMode] = null;
Expand All @@ -633,17 +639,24 @@ private function _disconnect($connectionMode)
$this->_connections[$connectionMode]->close();
}
}
catch(\Exception $e)
catch(Exception $e)
{
}
$this->_connections[$connectionMode] = null;
}

/**
* @param callable $callback
* @param $batchSize
*
* @return bool
* @throws QueueException
*/
public function batchConsume(callable $callback, $batchSize)
{
if($this->_qosCount && $batchSize > $this->_qosCount)
{
throw new \Exception('Cannot consume batches greater than QoS');
throw new QueueException('Cannot consume batches greater than QoS');
}
return parent::batchConsume($callback, $batchSize);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Provider/Google/GooglePubSubProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Google\Cloud\PubSub\Topic;
use Packaged\Queue\IBatchQueueProvider;
use Packaged\Queue\Provider\AbstractQueueProvider;
use Packaged\Queue\Provider\QueueCredentialsException;

/*
* Available config options:
Expand All @@ -19,6 +20,7 @@
* auto_create false If true then automatically create topics and subscriptions if they do not exist
* ack_deadline null Default ACK deadline for messages in this subscription. Uses Google's default if not specified.
*/

class GooglePubSubProvider extends AbstractQueueProvider implements IBatchQueueProvider
{
/** @var string */
Expand Down Expand Up @@ -69,7 +71,7 @@ private function _getClient()
* @param string|array $credentials
*
* @return array
* @throws \Exception
* @throws QueueCredentialsException
*/
private function _loadCredentials($credentials)
{
Expand All @@ -80,21 +82,21 @@ private function _loadCredentials($credentials)
{
if(!is_file($credentials))
{
throw new \Exception('The specified credentials file is not a file');
throw new QueueCredentialsException('The specified credentials file is not a file');
}
$credentials = file_get_contents($credentials);
}

$decoded = json_decode($credentials, true);
if(!$decoded)
{
throw new \Exception('The provided credentials are not in valid JSON format');
throw new QueueCredentialsException('The provided credentials are not in valid JSON format');
}
$credentials = $decoded;
}
if((!is_array($credentials)) || (empty($credentials['project_id'])))
{
throw new \Exception(('Invalid credentials provided'));
throw new QueueCredentialsException(('Invalid credentials provided'));
}
return $credentials;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Provider/QueueConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Packaged\Queue\Provider;

use Packaged\Queue\QueueException;

class QueueConnectionException extends QueueException
{
}
8 changes: 8 additions & 0 deletions src/Provider/QueueCredentialsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Packaged\Queue\Provider;

use Packaged\Queue\QueueException;

class QueueCredentialsException extends QueueException
{
}
8 changes: 8 additions & 0 deletions src/QueueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Packaged\Queue;

use Exception;

class QueueException extends Exception
{
}

0 comments on commit 6ba6a39

Please sign in to comment.