diff --git a/src/cluster/__tests__/connectionBuilder.spec.js b/src/cluster/__tests__/connectionBuilder.spec.js index d6f380723..a166544dd 100644 --- a/src/cluster/__tests__/connectionBuilder.spec.js +++ b/src/cluster/__tests__/connectionBuilder.spec.js @@ -1,6 +1,7 @@ const { newLogger } = require('testHelpers') const connectionBuilder = require('../connectionBuilder') const Connection = require('../../network/connection') +const { KafkaJSNonRetriableError } = require('../../errors') describe('Cluster > ConnectionBuilder', () => { let builder @@ -59,4 +60,40 @@ describe('Cluster > ConnectionBuilder', () => { expect(connection.port).toEqual(8888) expect(connection.rack).toEqual('rack') }) + + it('throws an exception if brokers list is empty', () => { + expect(() => { + builder = connectionBuilder({ + socketFactory, + brokers: [], + ssl, + sasl, + clientId, + connectionTimeout, + retry, + logger, + }) + }).toThrow( + KafkaJSNonRetriableError, + 'Failed to connect: expected brokers array and got nothing' + ) + }) + + it('throws an exception if brokers is null', () => { + expect(() => { + builder = connectionBuilder({ + socketFactory, + brokers: null, + ssl, + sasl, + clientId, + connectionTimeout, + retry, + logger, + }) + }).toThrow( + KafkaJSNonRetriableError, + 'Failed to connect: expected brokers array and got nothing' + ) + }) }) diff --git a/src/cluster/connectionBuilder.js b/src/cluster/connectionBuilder.js index f21d8970e..bef85f554 100644 --- a/src/cluster/connectionBuilder.js +++ b/src/cluster/connectionBuilder.js @@ -1,4 +1,11 @@ const Connection = require('../network/connection') +const { KafkaJSNonRetriableError } = require('../errors') + +const validateBrokers = brokers => { + if (!brokers || brokers.length === 0) { + throw new KafkaJSNonRetriableError(`Failed to connect: expected brokers array and got nothing`) + } +} module.exports = ({ socketFactory, @@ -14,6 +21,8 @@ module.exports = ({ logger, instrumentationEmitter = null, }) => { + validateBrokers(brokers) + const size = brokers.length let index = 0