Skip to content

Commit

Permalink
Always emit error events (fixes mqttjs#1939)
Browse files Browse the repository at this point in the history
  • Loading branch information
SystemParadox committed Sep 23, 2024
1 parent 3e252e7 commit dfe6f98
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,16 +798,9 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
}

const streamErrorHandler = (error) => {
// Emit all errors (see #1939)
this.log('streamErrorHandler :: error', error.message)
// error.code will only be set on NodeJS env, browser don't allow to detect errors on sockets
// also emitting errors on browsers seems to create issues
if (error.code) {
// handle error
this.log('streamErrorHandler :: emitting error')
this.emit('error', error)
} else {
this.noop(error)
}
this.emit('error', error)
}

this.log('connect :: pipe stream to writable stream')
Expand Down
9 changes: 9 additions & 0 deletions test/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,15 @@ export default function abstractTest(server, config, ports) {
})
})

it('should emit error events from invalid connection options', function _test(t, done) {
const client = connect({ password: 'xxx' })

client.on('error', (e: any) => {
assert.equal(e.message, 'Username is required to use password')
client.end((err) => done(err))
})
})

it('should have different client ids', function _test(t, done) {
// bug identified in this test: the client.end callback is invoked twice, once when the `end`
// method completes closing the stores and invokes the callback, and another time when the
Expand Down

0 comments on commit dfe6f98

Please sign in to comment.