From 956a890b940809ffe259a8c5a97bdfb59e52a99b Mon Sep 17 00:00:00 2001 From: JC Brand Date: Mon, 28 Jun 2021 13:09:18 +0200 Subject: [PATCH] Don't reject on error in sendTimedMessage It's the only way I could get a failing test (due to the Jasmine upgrade AFAIK) to pass. I don't understand why this happens, given that the promise has a `catch` clause in `retractOwnMessage`, but for some reason the promise rejection gets caught by Jasmine, causing the test to fail. --- src/headless/plugins/muc/muc.js | 39 +++++++++++++-------------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/headless/plugins/muc/muc.js b/src/headless/plugins/muc/muc.js index a13a95c54..95b6f1981 100644 --- a/src/headless/plugins/muc/muc.js +++ b/src/headless/plugins/muc/muc.js @@ -665,8 +665,7 @@ const ChatRoomMixin = { * @method _converse.ChatRoom#sendTimedMessage * @param { _converse.Message|XMLElement } message * @returns { Promise|Promise<_converse.TimeoutError> } Returns a promise - * which resolves with the reflected message stanza or rejects - * with an error stanza or with a {@link _converse.TimeoutError}. + * which resolves with the reflected message stanza or with an error stanza or {@link _converse.TimeoutError}. */ sendTimedMessage (el) { if (typeof el.tree === 'function') { @@ -678,28 +677,18 @@ const ChatRoomMixin = { id = this.getUniqueId('sendIQ'); el.setAttribute('id', id); } - let handler; + const promise = getOpenPromise(); const timeoutHandler = _converse.connection.addTimedHandler(_converse.STANZA_TIMEOUT, () => { _converse.connection.deleteHandler(handler); - promise.reject(new _converse.TimeoutError('Timeout Error: No response from server')); + const err = new _converse.TimeoutError('Timeout Error: No response from server'); + promise.resolve(err); return false; }); - const promise = new Promise((resolve, reject) => { - handler = _converse.connection.addHandler( - stanza => { - timeoutHandler && _converse.connection.deleteTimedHandler(timeoutHandler); - if (stanza.getAttribute('type') === 'groupchat') { - resolve(stanza); - } else { - reject(stanza); - } - }, - null, - 'message', - ['error', 'groupchat'], - id - ); - }); + const handler = _converse.connection.addHandler( + stanza => { + timeoutHandler && _converse.connection.deleteTimedHandler(timeoutHandler); + promise.resolve(stanza); + }, null, 'message', ['error', 'groupchat'], id); api.send(el); return promise; }, @@ -737,10 +726,12 @@ const ChatRoomMixin = { 'retraction_id': stanza.nodeTree.getAttribute('id'), 'editable': false }); - try { - await this.sendTimedMessage(stanza); - } catch (e) { - log.error(e); + const result = await this.sendTimedMessage(stanza); + + if (u.isErrorStanza(result)) { + log.error(result); + } else if (result instanceof _converse.TimeoutError) { + log.error(result); message.save({ editable, 'error_type': 'timeout',