From dfe6f986cab4d9fb12072405b7821b422624a9b8 Mon Sep 17 00:00:00 2001 From: Simon Williams Date: Mon, 23 Sep 2024 12:14:14 +0100 Subject: [PATCH] Always emit error events (fixes #1939) --- src/lib/client.ts | 11 ++--------- test/abstract_client.ts | 9 +++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/lib/client.ts b/src/lib/client.ts index 8d11e8e2d..9a47e1ba6 100644 --- a/src/lib/client.ts +++ b/src/lib/client.ts @@ -798,16 +798,9 @@ export default class MqttClient extends TypedEventEmitter { + // 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') diff --git a/test/abstract_client.ts b/test/abstract_client.ts index df468b69c..777a0e514 100644 --- a/test/abstract_client.ts +++ b/test/abstract_client.ts @@ -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