From 0bc532bfd3f04385fc92d184d6be4050fd1b1ae3 Mon Sep 17 00:00:00 2001 From: Francois Date: Tue, 23 Mar 2021 19:33:36 +0100 Subject: [PATCH 1/2] Test if connect() throw an unhandled exception I just find an issue with connect. When called on certain server the connection failed. It throw correctly the error but an unhandle promise is also rejected. it should not be cough by the system and should be normally trapped. I didn't investigate much more at the moment as I spend more time to reproduce the issue and create a test case. I will come back with more detail to understand why connecting on that server trigger the `unhandledRejection` event. --- src/client-integration.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/client-integration.js b/src/client-integration.js index 76ccbe3e..b1c2beae 100644 --- a/src/client-integration.js +++ b/src/client-integration.js @@ -146,6 +146,35 @@ describe('browserbox integration tests', () => { return imap.close() }) }) + + it('should not throw unhandled rejection when connecting on a wrong server', function () { + return new Promise((resolve, reject) => { + process.on('unhandledRejection', reject) + + imap = new ImapClient('bluehope.xyz', 465, { + logLevel, + auth: { + user: 'invalid', + pass: 'invalid' + }, + useSecureTransport: true + }) + + imap.connect().then(() => { + expect(false) + }).catch((err) => { + expect(err).to.be.an('error') + expect(err.message).to.include('Unexpected char at position') + }).then(() => { + // must wait after current loop otherwise unhandledRejection is called after the end of the test. + return new Promise((resolve) => setImmediate(resolve)) + }).then(() => { + process.off('unhandledRejection', reject) + }).then(() => { + resolve() + }) + }) + }) it('should fail authentication', (done) => { imap = new ImapClient('127.0.0.1', port + 2, { From c2ba780e1b79eee126fb809c04d93020c183007b Mon Sep 17 00:00:00 2001 From: Francois Date: Sat, 27 Mar 2021 15:52:37 +0100 Subject: [PATCH 2/2] fix unhandled exception on failed updateCapability When `updateCapability` failed, the reject is not call and an unhandled exception is throw --- src/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.js b/src/client.js index f9638516..710bd53e 100644 --- a/src/client.js +++ b/src/client.js @@ -160,7 +160,7 @@ export default class Client { clearTimeout(connectionTimeout) this._changeState(STATE_NOT_AUTHENTICATED) this.updateCapability() - .then(() => resolve(this._capability)) + .then(() => resolve(this._capability), reject) } this.client.onerror = (err) => {