I wrote a custom error logging method and I'm implementing it where things could go wrong. For RabbitMQ however, it's not clear for me how to check for an error. This is my code
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
$errorServiceObject = new ErrorService();
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();
if(!isset($channel))
{
$errorServiceObject->LogError('RabbitMQ error.', 'Could not initiate a RabbitMQ connection.');
exit();
}
The way I do it at the moment is bad because it only checks whether the variable $channel
is set or when the connection is simply not initiated correctly. I would like to check whether the new AMQPStreamConnection
information are valid or not. Consider this example where I do this
$connection = new AMQPStreamConnection('localhostWithATypo', 5444, 'guest', 'guest'); //wrong hostname and wrong port number
. What would I need to put in the if statement to trigger the error handeling function?