Skip to content

Commit dfe6f98

Browse files
committed
Always emit error events (fixes mqttjs#1939)
1 parent 3e252e7 commit dfe6f98

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/lib/client.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -798,16 +798,9 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
798798
}
799799

800800
const streamErrorHandler = (error) => {
801+
// Emit all errors (see #1939)
801802
this.log('streamErrorHandler :: error', error.message)
802-
// error.code will only be set on NodeJS env, browser don't allow to detect errors on sockets
803-
// also emitting errors on browsers seems to create issues
804-
if (error.code) {
805-
// handle error
806-
this.log('streamErrorHandler :: emitting error')
807-
this.emit('error', error)
808-
} else {
809-
this.noop(error)
810-
}
803+
this.emit('error', error)
811804
}
812805

813806
this.log('connect :: pipe stream to writable stream')

test/abstract_client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,15 @@ export default function abstractTest(server, config, ports) {
477477
})
478478
})
479479

480+
it('should emit error events from invalid connection options', function _test(t, done) {
481+
const client = connect({ password: 'xxx' })
482+
483+
client.on('error', (e: any) => {
484+
assert.equal(e.message, 'Username is required to use password')
485+
client.end((err) => done(err))
486+
})
487+
})
488+
480489
it('should have different client ids', function _test(t, done) {
481490
// bug identified in this test: the client.end callback is invoked twice, once when the `end`
482491
// method completes closing the stores and invokes the callback, and another time when the

0 commit comments

Comments
 (0)