Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #454 proposal - Adding validation to connectionBuilder #460

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/cluster/__tests__/connectionBuilder.spec.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'
)
})
})
9 changes: 9 additions & 0 deletions src/cluster/connectionBuilder.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,6 +21,8 @@ module.exports = ({
logger,
instrumentationEmitter = null,
}) => {
validateBrokers(brokers)

const size = brokers.length
let index = 0

Expand Down