Skip to content

Commit

Permalink
emit client errors like unauthorized (#204)
Browse files Browse the repository at this point in the history
* emit client errors like unauthorized

* move messages to the top
  • Loading branch information
behrad authored and mcollina committed Jun 7, 2018
1 parent 035dc98 commit f1f7e8d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/handlers/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ var connectActions = [
emptyQueue
]

var errorMessages = [
'',
'unacceptable protocol version',
'identifier rejected',
'Server unavailable',
'bad user name or password',
'not authorized'
]

function handleConnect (client, packet, done) {
client.connected = true
client.clean = packet.clean
Expand Down Expand Up @@ -60,28 +69,35 @@ function authenticate (arg, done) {
negate)

function negate (err, successful) {
var errCode
if (!err && successful) {
client.broker.registerClient(client)
return done()
} else if (err) {
if (err.returnCode && (err.returnCode >= 1 && err.returnCode <= 3)) {
errCode = err.returnCode
write(client, {
cmd: 'connack',
returnCode: err.returnCode
}, client.close.bind(client, done))
} else {
// If errorCode is 4 or not a number
errCode = 4
write(client, {
cmd: 'connack',
returnCode: 4
}, client.close.bind(client, done))
}
} else {
errCode = 5
write(client, {
cmd: 'connack',
returnCode: 5
}, client.close.bind(client, done))
}
var error = new Error(errorMessages[errCode])
error.errorCode = errCode
client.broker.emit('clientError', client, error)
}
}

Expand Down

0 comments on commit f1f7e8d

Please sign in to comment.