From 58b79b40edfdd39ab02e636388de15037eb0a48d Mon Sep 17 00:00:00 2001 From: Tim Haley Date: Wed, 27 Jul 2022 15:54:44 -0400 Subject: [PATCH] Ensure callback is called when envoking errorHanlder A property, errorHanlder, was recently added which will be called instead of throwing an error in the .flush() method. This is important because errors in .flush() could, previously, only be handled via process.on('uncaughtException', err => { ... }). However, this property is currently unusable as, when the flush method invokes this property, it fails to call the callbacks of the events being flushed. This commit makes sure the callbacks are called. --- index.js | 1 + test.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/index.js b/index.js index 60c8bc18..66a8427f 100644 --- a/index.js +++ b/index.js @@ -298,6 +298,7 @@ class Analytics { }) .catch(err => { if (typeof this.errorHandler === 'function') { + done(err) return this.errorHandler(err) } diff --git a/test.js b/test.js index 34766e22..9dd33ce4 100644 --- a/test.js +++ b/test.js @@ -381,6 +381,23 @@ test('flush - do not throw on axios failure if errorHandler option is specified' t.true(errorHandler.calledOnce) }) +test('flush - evoke callback when errorHandler option is specified', async t => { + const errorHandler = spy() + const client = createClient({ errorHandler }) + const callback = spy() + + client.queue = [ + { + message: 'error', + callback + } + ] + + await t.notThrows(client.flush()) + await delay(5) + t.true(callback.calledOnce) +}) + test('flush - time out if configured', async t => { const client = createClient({ timeout: 500 }) const callback = spy()