Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.

Commit fb41963

Browse files
authored
Merge pull request #342 from tam-carre/master
Add an errorHandler property to constructor options
2 parents 2787295 + 81024d9 commit fb41963

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Analytics {
2929
* @property {Object} [axiosInstance] (default: axios.create(options.axiosConfig))
3030
* @property {Object} [axiosRetryConfig] (optional)
3131
* @property {Number} [retryCount] (default: 3)
32+
* @property {Function} [errorHandler] (optional)
3233
*/
3334

3435
constructor (writeKey, options) {
@@ -50,6 +51,7 @@ class Analytics {
5051
this.maxQueueSize = options.maxQueueSize || 1024 * 450 // 500kb is the API limit, if we approach the limit i.e., 450kb, we'll flush
5152
this.flushInterval = options.flushInterval || 10000
5253
this.flushed = false
54+
this.errorHandler = options.errorHandler
5355
Object.defineProperty(this, 'enable', {
5456
configurable: false,
5557
writable: false,
@@ -295,6 +297,10 @@ class Analytics {
295297
return Promise.resolve(data)
296298
})
297299
.catch(err => {
300+
if (typeof this.errorHandler === 'function') {
301+
return this.errorHandler(err)
302+
}
303+
298304
if (err.response) {
299305
const error = new Error(err.response.statusText)
300306
done(error)

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,22 @@ test('flush - respond with an error', async t => {
365365
await t.throws(client.flush(), 'Bad Request')
366366
})
367367

368+
test('flush - do not throw on axios failure if errorHandler option is specified', async t => {
369+
const errorHandler = spy()
370+
const client = createClient({ errorHandler })
371+
const callback = spy()
372+
373+
client.queue = [
374+
{
375+
message: 'error',
376+
callback
377+
}
378+
]
379+
380+
await t.notThrows(client.flush())
381+
t.true(errorHandler.calledOnce)
382+
})
383+
368384
test('flush - time out if configured', async t => {
369385
const client = createClient({ timeout: 500 })
370386
const callback = spy()

0 commit comments

Comments
 (0)