From 46b64e306f714411017603597c0918cf5a2cc741 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 30 Aug 2017 13:33:55 -0700 Subject: [PATCH] createTaskWithTimeout: Don't log expiration if task threw (#1412) FREEBIE --- js/libtextsecure.js | 8 +++++++- libtextsecure/task_with_timeout.js | 8 +++++++- libtextsecure/test/task_with_timeout_test.js | 18 ++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 386c63250ae..d2ffae84b12 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -40272,7 +40272,13 @@ libsignal.ProvisioningCipher = function() { return reject(error); }; - var promise = task(); + var promise; + try { + promise = task(); + } catch(error) { + clearTimer(); + throw error; + } if (!promise || !promise.then) { clearTimer(); complete = true; diff --git a/libtextsecure/task_with_timeout.js b/libtextsecure/task_with_timeout.js index ee2ab363fb6..d2cdd6c942c 100644 --- a/libtextsecure/task_with_timeout.js +++ b/libtextsecure/task_with_timeout.js @@ -51,7 +51,13 @@ return reject(error); }; - var promise = task(); + var promise; + try { + promise = task(); + } catch(error) { + clearTimer(); + throw error; + } if (!promise || !promise.then) { clearTimer(); complete = true; diff --git a/libtextsecure/test/task_with_timeout_test.js b/libtextsecure/test/task_with_timeout_test.js index 65e86b2d759..713d94c58ae 100644 --- a/libtextsecure/test/task_with_timeout_test.js +++ b/libtextsecure/test/task_with_timeout_test.js @@ -22,13 +22,13 @@ describe('createTaskWithTimeout', function() { assert.strictEqual(error, flowedError); }); }); - it('rejects if promise takes too long', function() { + it('rejects if promise takes too long (this one logs error to console)', function() { var error = new Error('original'); var complete = false; var task = function() { return new Promise(function(resolve) { setTimeout(function() { - completed = true; + complete = true; resolve(); }, 3000); }); @@ -57,4 +57,18 @@ describe('createTaskWithTimeout', function() { assert.strictEqual(result, 'hi!') }); }); + it('rejects if task throws (and does not log about taking too long)', function() { + var error = new Error('Task is throwing!'); + var task = function() { + throw error; + }; + var taskWithTimeout = textsecure.createTaskWithTimeout(task, this.name, { + timeout: 10 + }); + return taskWithTimeout().then(function(result) { + throw new Error('Overall task should reject!') + }, function(flowedError) { + assert.strictEqual(flowedError, error); + }); + }); });