I want to check whether a particular RabbitMQ exchange exists or not from node.js. I am using Mocha as test framework. I have written code for the same but my expectation seems to be incorrect. I expect exchange variable to have a value of undefined when there is no exchange, but that is not the case. I am using amqp module for interacting with RabbitMQ. The following is the code:
var should = require('should');
var amqp = require('amqp');
//Configuration
var amqpConnectionDetails = {
'host':'localhost',
'port':5672,
'login':'guest',
'password':'guest'
};
describe('AMQP Objects', function(){
describe('Exchanges', function(){
it('There should exist an exchange', function(done){
var amqpConnection = amqp.createConnection(amqpConnectionDetails);
amqpConnection.on('ready', function(){
var exchange = amqpConnection.exchange('some_exchange', {'passive':true, 'noDeclare':true});
exchange.should.not.be.equal(undefined);
exchange.should.not.be.equal(null);
done();
});
});
});
});
What is the right way to check for the existence of an exchange?
Thanks.
If the exchange does not exist, an error will be thrown ("Uncaught Error: NOT_FOUND - no exchange 'some_exchange' in vhost '/'"). This means that you should add an "on error" method to exchange to catch the error that it will throw when the exchange does not exist.
Second, you should remove 'noDeclare':true from your options.
The following should work (it will exit gracefully if the exchange does not exist and will throw an exception if the exchange does exist):